Skip to content

Instantly share code, notes, and snippets.

@jhannah
Last active February 6, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhannah/591c8415c3558bcabc745b423c254a6d to your computer and use it in GitHub Desktop.
Save jhannah/591c8415c3558bcabc745b423c254a6d to your computer and use it in GitHub Desktop.
@_ is easy(tm)
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);
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