Last active
February 6, 2019 21:22
-
-
Save jhannah/591c8415c3558bcabc745b423c254a6d to your computer and use it in GitHub Desktop.
@_ is easy(tm)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.26.0; | |
package Foo; | |
sub new { bless {} } | |
sub bar { | |
my $baz = sub { | |
return join " ", @_; | |
}; | |
say "Current code: " . $baz->(); | |
say "Proposed code: " . $baz->(@_); | |
} | |
package main; | |
my $foo = Foo->new; | |
$foo->bar(3,4,5); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Foo; | |
use Modern::Perl; | |
use DDP; | |
sub new { | |
say '@_ is ', np( @_ ); | |
my $class = shift; | |
my $self = { @_ }; | |
bless( $self, $class ); | |
say "Instantiating $class as ", np( $self ); | |
return( $self ); | |
} | |
sub say_hello { | |
say "Hello"; | |
} | |
sub generate_coderef { | |
say '@_ entering ', (caller(0))[3], ' is ', np( @_ ); | |
my $self = shift; | |
say '@_ in ', (caller(0))[3], ' after shift is ', np( @_ ); | |
my $coderef = sub { | |
say '@_ entering $coderef ', (caller(0))[3], ' is ', np( @_ ); | |
$self->say_hello; | |
} | |
} | |
my $foo = new Foo(1=>2, 3=>4); | |
my $coderef = $foo->generate_coderef(11,22,33); | |
$coderef->(); | |
$coderef->(44,55,66); | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment