in reply to reference to a subroutine inside a package
does not actually put a 'foo' method into Bar. Instead, when you do this:package Foo; sub foo(); sub new { bless[] } package Bar; our @ISA = qw(Foo); sub new { bless[] }
Perl calls an internal function called call_method() documented in perlapi to call $b->foo(). call_method() does this:$b = new Bar; $b->foo();
Note that this mechanism plays funnily with things that work by placing specially named methods into packages (specifically, overloading).
Now with that explanation out of the way, there are two ways you can get at that method.
The first one was given by someone else:
If you really need a reference to the method, that's the only way to do it.$sub = $obj->can('foo'); # $sub references the sub that would be called if you # called $obj->foo(), handling inheritance and everything. # In fact, Perl has enough magic that if everyone plays # their cards right, it will work for autoloaded methods # that haven't even been loaded yet. # ...a bit later... # $obj->$sub(@args); # same as $obj->foo(@args);
However, there's another alternative that has mysteriously vanished from the perl 5.8 documentation (it was present in 5.6); if you only care about the method itself, not the address, this will work equally well:
$meth = 'foo'; $obj->$meth(@args); # same as $obj->foo(@args)
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
|
|---|