[pm-h] sigs

Reini Urban reini.urban at gmail.com
Wed Feb 19 21:31:05 PST 2014


On Wed, Feb 19, 2014 at 2:35 PM, Mark Allen <mrallen1 at yahoo.com> wrote:
> When subroutine signatures are released in 5.20, you'll be able to do
>
> sub foo ($self, $foo, $bar, $baz) {
>    $foo ||= 'default';
>    $baz //= 0;
>
>    ...;
> }
>
> Yay!


On Feb 19, 2014, at 2:35 PM, Mark Allen <mrallen1 at yahoo.com> wrote:

> When subroutine signatures are released in 5.20, you'll be able to do
>
>sub foo ($self, $foo, $bar, $baz) {
>   $foo ||= 'default';
>   $baz //= 0;
>   ...;
>}

That can actually be written as:

$ perl5.19.9 -Mexperimental=signatures -E'
sub foo ($self, $foo, $bar="default", $baz=0) {print $foo, $bar} foo(0,1);
'

=> 1default

defaults in the sig declaration

> For those who can't imagine why you would do such a thing, it often
> happens when a sub returns a "list" (which it is in list context) and
> the calling code assigns it to a scalar. This completely changes how
> the return is interpreted.
>
>> I actually don't like the current popular practice of doing
>>
>> my ($self, $a, $b, $c)=@_;
>>
>> I much prefer
>>
>> my $self = shift;
>> my $a    = shift;
>> my $b    = shift;
>>
>> as I can
>>
>> my $self = shift;
>> my $a    = shift || "default";
>> my $b    = shift // 0;
>>
>> and actually understand what I did months later.
>
> Although it only matters in a few circumstances, a single 'my'
> statement is marginally faster than multiples. I used to do the
> separate statement thing, but now I tend to:
>
> my ($self, $foo, $bar, $baz) = @_;
> $foo ||= "default";
> $baz //= 0;
>
> I vaguely remember something about a cost for the shift, but that may
> have been fixed.

As said, defaults are now possible within the sig.
The remaining problem is the cost to fill @_ at all, as with such sigs
@_ is only needed for optional &rest args. in perl6-speek: slurpy

But p5p decided to keep @_ around, which is performance hit and a
backwards compat problem for 5.22.

-- 
Reini Urban
http://phpwiki.org/           http://murbreak.at/


More information about the Houston mailing list