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

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

Thread Previous | Thread Next
Branislav ZahradnĂ­k
September 5, 2023 09:00
Re: //= and ||= in subroutine signatures
Message ID:
On Sun, 3 Sept 2023 at 10:48, Ivan Vorontsov <> wrote:

> With perl v5.38.0 I wrote a code similar to:
>     perl -E 'sub foo ($x, $y //= 42, $z) {} foo(1, undef, 3)'
> and got the error:
>     Mandatory parameter follows optional parameter at -e line 1, near "$z)
> "
> So //= and ||= mark a parameter as optional, but optional parameters must
> follow mandatory ones. Outside of signatures these operators check a value
> of a variable. A mandatory parameter can have undef or false value. If it
> is feasible, maybe these operators could be applied to mandatory parameters
> as well.
you are mixing two domains not clearly formulated yet (in Perl)

one is signatures, where operators acts like "if not exists"
second is data contract, where you want "use 42 if undef"

first one struggles with missing concept of "not-exists" value
for second, you can use Type::Params for now, maybe current WIP (Oshun)
will solve it in future

> --
> Ivan Vorontsov <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About