Created
July 12, 2009 01:57
-
-
Save lestrrat/145481 to your computer and use it in GitHub Desktop.
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
# モンキーパッチングは万人におすすめできる手法とは思えないのでやらないほうがいいと思うなぁ - lestrrat | |
# http://d.hatena.ne.jp/bingo_nakanishi_perl/20090711/1247331243 | |
package Bar; | |
use strict; | |
sub new { | |
my($class, %args) = @_; | |
return bless { %args }, $class; | |
} | |
sub say { | |
my $self = shift; | |
print $self->{name}, "です\n"; | |
} | |
package main; | |
my $bar = Bar->new(name => "パー"); | |
$bar->say(); | |
# そのままsubをここで宣言してもいいけど、それだとコンパイルフェーズで走るので、 | |
# 本当の意味で動的に作成するならeval | |
eval <<'EOSUB'; | |
sub Bar::say2 { | |
my $self = shift; | |
print $self->{name}, "やがな\n"; | |
} | |
EOSUB | |
$bar->say2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment