develooper Front page | perl.perl5.porters | Postings from January 2022

Re: Things you can't do in a signatured sub

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
January 24, 2022 14:59
Subject:
Re: Things you can't do in a signatured sub
Message ID:
20220124145924.2f687194@shy.leonerd.org.uk
On Mon, 24 Jan 2022 07:01:46 +0000 (UTC)
Ovid via perl5-porters <perl5-porters@perl.org> wrote:

> Imagine if we had a $^NUM_ARGS
> variable (terrible name, though I think someone else suggested it):
> 
>     sub name ( $self, $name=undef ) {
>         if ( $^NUM_ARGS == 2) {
>             $self->{name} = $name; # even if undef
>         }
>         else {
>             return $self->{name};
>         }
>     }

I've often suggested it as `argc` or variations thereon:

On Tue, 7 Dec 2021 18:02:23 +0000
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> In these situations I think it could be quite easy to
> permit a `builtin::argc` to allow it:
> 
>   use builtin 'argc';
> 
>   sub red($self, $new = undef) {
>      $self->{red} = $new if argc > 1;
>      return $self->{red};
>   }

https://www.nntp.perl.org/group/perl.perl5.porters/2021/12/msg262142.html

Dave M has rather strongly objected to it:

On Thu, 9 Dec 2021 15:01:21 +0000
Dave Mitchell <davem@iabyn.com> wrote:

> using the arg count as a way of determining whether a default arg
> was passed etc, is awkward, ugly and error-prone for all but the
> simplest subs:
> 
>     sub foo ($self, $base, $action, $flags, $result, $x, $y = 0, $z =
> 0) { my @point = ($x);
>         push @point, $y if @_ > 6;
>         push @point, $z if @_ > 7;
>         $self->do_point(@point);
>         ....
>     }
> 
> whereas the query params proposal allows:
> 
> 
>     sub foo ($self, $base, $action, $flags, $result,
>             ?@point,
>             $x, $y = 0, $z = 0)
>         {
>         $self->do_point(@point);
>         ....
>     }

https://www.nntp.perl.org/group/perl.perl5.porters/2021/12/msg262156.html

-- 
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