method foo(\@bar) { print @bar }

schwern on 2008-09-21T21:23:50

New version of Method::Signatures on its way to CPAN, now with reference aliasing.

package Stuff;

use Method::Signatures;

method foo(\@bar) { $_++ for @bar; }

my @list = (1,2,3); Stuff->foo(\@list); print "@list"; # 2 3 4


No more having to muck around with reference syntax.

This currently incurs a 20% performance penalty vs an empty subroutine using the normal argument passing techniques. But once that cost is paid there's no further cost for using the aliasing, so for a method of any size it won't matter. You only pay that cost on methods which use it.

How does it work? My first thought was to use Data::Bind but that slowed down methods by about 400x. Now it's just an our/local hack. The above is equivalent to...

sub foo {
    my $self = shift;
    our(@bar);
    local(@bar);
    *bar = $_[0];
    $_++ for @bar;
}


...except I just noticed closures don't work.

Crap.

*UPDATE* Using Data::Alias, 0.05 eliminates the performance penalty and closures work.