Fellow Monks,

Some background first (skip next two paragraphs if you don't care):

I have a class (Foo) that reads in an XML-style configuration file and makes the information available to the caller via a bunch of AUTOLOADed methods. This worked fine for a while, but then the amount of data within Foo got larger, and the number of people maintaining Foo also increased, and it became unmanagable to keep all the parsing and accessing within the same class. I therefore rewrote Foo as a container, with various block objects to parse and store the different sections of the config. In other words, Foo is now little more than an array of block objects with an enumerator, and the block objects do the real work. So far, so good.

I now need to take the AUTOLOADed methods that Foo catches and call them on the current block object. So if a caller does something like $myFoo->Bar(), I need to call the Bar method of the current block. I should mention that the blocks exist within a class hierarchy, so the methods that the class implements might not be in the class package. Ick.

The Problem: I need to call dynamically determined methods on an object, where the method name is contained in a string. As an additional complication, the method might not be in the class receiving the call; it can be in one of its superclasses. An example might help:

Say I have the following class in MyClass.pm:

package MyClass; use strict; sub new($) { my $invocant = shift; my $class = ref($invocant) || $invocant; # object or class name my $self = { }; bless($self, $class); return $self; } sub HelloWorld($) { my $self = shift; print "Hello, World!\n"; } 1;
and I have code calling the class like this:
use MyClass; use strict; my $pack = MyClass->new(); my $string = "HelloWorld"; $string = ref($pack) . "::$string"; no strict 'refs'; &$string($pack);
This works fine. Now suppose that I have a subclass of MyClass named MySubClass (in MySubClass.pm), which looks like this:
package MySubClass; use strict; our @ISA = qw(MyClass); require MyClass; # All implementation is done by the superclass 1;
and I change the test code to use MySubClass instead of MyClass:
use MySubClass; use strict; my $pack = MySubClass->new(); my $string = "HelloWorld"; $string = ref($pack) . "::$string"; no strict 'refs'; &$string($pack);
This will fail with the message "Undefined subroutine &MySubClass::HelloWorld called at test.pl line 9". It appears that perl is looking for HelloWorld within MySubClass.pm, and won't look in packages specified in the @ISA array. Incedentally, calls like "$pack->HelloWorld()" work fine.

So my question is the following: Is there some way I can get method behavior (looking though the namespaces in the @ISA package) when I don't know the name of the method until runtime? Or do I have to look through the @ISA array myself?

Any help would be appreciated,

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...


In reply to Dynamic Method Calls by ton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.