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

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

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
September 5, 2023 21:12
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
20230905221238.7b79e69c@shy.leonerd.org.uk
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;
      ...
   }

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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