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

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

Thread Previous | Thread Next
From:
Ivan Vorontsov
Date:
September 4, 2023 15:26
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
20230904182557.1ff876365ff8afc84f8b8417@yandex.ru
>     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 <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