As others have said, this sort of thing:
package Foo;
sub foo();
sub new { bless[] }
package Bar;
our @ISA = qw(Foo);
sub new { bless[] }
does not actually put a 'foo' method into Bar. Instead, when you do this:
$b = new Bar;
$b->foo();
Perl calls an internal function called
call_method() documented in
perlapi to call
$b->foo().
call_method() does this:
- Looks in the package the object is blessed into for a sub with that name. If one is found, great! That sub is called.
- Otherwise, recursively searches packages listed in @ISA for the given sub. If the sub is found, it is called.
- Otherwise, checks the magic UNIVERSAL package for the method. (Note that UNIVERSAL is only magic for call_method; you can add new global object methods by adding them to the UNIVERSAL package!)
- Failing that, the blessed object's package and its @ISA are scanned again; however, instead of looking for the specified method, it looks for a method named AUTOLOAD. (This can interfere when you want to use autoloaded methods, and you inherit from a class that uses autoloaded.) Note that UNIVERSAL::AUTOLOAD will be called if it exists.
- If Perl finally gets to this step, your program will croak with "Can't locate object methd 'foo' via package 'Bar' at...".
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:
$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);
If you really need a reference to the method, that's the only way to do it.
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)
--Stevie-O
$"=$,,$_=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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.