Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

schwern (1528)

  (email not shown publicly)
AOL IM: MichaelSchwern (Add Buddy, Send Message)

Schwern can destroy CPAN at his whim.

Journal of schwern (1528)

Sunday September 21, 2008
04:23 PM

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

[ #37505 ]

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);
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;
    *bar = $_[0];
    $_++ for @bar;

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


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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.