Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-18 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found