Emulated implicit method invocation
作者:gugod 發佈於:I just realized how easy it is, in Perl 5, to emulate a implicit method invocation given a '$self' is around:
sub foo {
my ($self) = @_;
# make this the same as $self->bar(1, 2, 3)
bar(1, 2, 3);
}
use PadWalker qw(peek_my);
sub bar {
# get '$self' from caller scope.
if (ref($_[0]) ne __PACKAGE__) {
my $caller_self = peek_my(1)->{'$self'};
unshift @_, $$caller_self;
}
my ($self) = @_;
...;
}
Apparently it'll be nice to have it in Rubyish, and/or self.pm