Created
February 19, 2015 07:22
-
-
Save niratama/58f6bd2cf570d75c2512 to your computer and use it in GitHub Desktop.
呼び出し元のサブルーチンを呼び出す(coderef版)
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use MyObj; | |
use MySub; | |
my $callback = sub { | |
print "in main::callback($_[0])\n"; | |
}; | |
MySub::test_sub('from main'); | |
{ | |
my $callback = sub { | |
print "in main2::callback($_[0])\n"; | |
}; | |
MySub::test_sub('from main2'); | |
} | |
my $obj1 = MyObj->new('obj1'); | |
my $obj2 = MyObj->new('obj2'); | |
$obj1->method('from main'); | |
$obj2->method('from main'); |
This file contains hidden or 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 MyObj; | |
use strict; | |
use warnings; | |
sub new { | |
my $class = shift; | |
return bless { name => shift }, $class; | |
} | |
sub method { | |
my $self = shift; | |
my $callback = sub { | |
my $name = $self->{name}; | |
print "in MyObj::callback($_[0]) / $name\n"; | |
}; | |
my $name = $self->{name}; | |
print "in MyObj($name)->method($_[0])\n"; | |
MySub::test_sub("from MyObj($name)"); | |
} | |
1; | |
This file contains hidden or 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 MySub; | |
use strict; | |
use warnings; | |
use PadWalker qw(peek_my); | |
use MySub2; | |
sub test_sub { | |
print "in MySub::test_sub($_[0])\n"; | |
my $callback = sub { | |
print "in MySub::callback($_[0])\n"; | |
}; | |
MySub2::test_sub('from MySub'); | |
${peek_my(1)->{'$callback'}}->('from MySub'); | |
} | |
1; | |
This file contains hidden or 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 MySub2; | |
use strict; | |
use warnings; | |
use PadWalker qw(peek_my); | |
sub test_sub { | |
print "in MySub2::test_sub($_[0])\n"; | |
my $callback = sub { | |
print "in MySub2::callback($_[0])\n"; | |
}; | |
${peek_my(1)->{'$callback'}}->('from MySub2'); | |
} | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment