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

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

Thread Previous | Thread Next
Ivan Vorontsov
September 4, 2023 15:26
Re: //= and ||= in subroutine signatures
Message ID:
>     f($x, $y //= 1,  $z = 2)    # $y is optional parameter

This is an intermediate case. To a caller $y is optional, but $y
must have undef value either explicitly or implicitly to trigger the
assignment, $z must not accept argument at all to get assigned. I would
call $y semi-optional :)

> I think //= and ||= should be consistently either mandatory or optional,
> and not vary based on context.

I think that it doesn't matter whether a parameter is mandatory or
optional. //= and ||= operators work by checking a value. It's a
conditional assignment to a variable that must already have a value to
be examined. It is a nice feature to have, a convenient shortcut, which
now works only in case of optional parameters.

If it can't be done, it's okay. I can write the check explicitly (as
usual :)):
    sub foo ($x, $y, $z) { $y //= 42 }

Ivan Vorontsov <>

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