develooper Front page | perl.perl5.porters | Postings from September 2023

Re: //= and ||= in subroutine signatures

Thread Previous | Thread Next
From:
Ivan Vorontsov
Date:
September 5, 2023 21:24
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
20230906002444.746a9002106667e66b601106@yandex.ru
On Tue, 5 Sep 2023 22:12:38 +0100 "Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> On Tue, 5 Sep 2023 22:55:46 +0300
> Ivan Vorontsov <ivrntsv@yandex.ru> wrote:
> 
> > Wouldn't it be convenient to write as:
> >     # $v, $w, $x are mandatory; $y, $z are optional
> >     sub bar ($v, $w //= 42, $x, $y //= 1, $z = 2) {
> >     }
> 
> If you want $w to be mandatory, you can't put a defaulting expression
> on it.
> 
> If you still want its value to be 42 if the caller *explicitly decided
> to pass undef*, that's that regular code inside the function is for.
> 
>    sub bar ($v, $w, $x, $y //= 1, $z = 2) {
>       $w //= 42;
>       ...
>    }

Accepted.

-- 
Ivan Vorontsov <ivrntsv@yandex.ru>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About