Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Hello,

Lets say I have a module called Object that has 2 subs (sub1
sub2) that return values (blah1 blah2).  In the script that uses
the module I have an array with the modules subroutine names.
I want to go through the array and execute the subroutines in
the object:

use Object;

@subs = qw(sub1 sub2);

$obj = new Object;

for (@subs){

  print $obj->$_ . "\n";

} 

Of course the above fails. I have tried using eval but must 
not be using it correctly.  Any info as to what I'm doing 
wrong would be greatly appreciated.

Thanks
TG

  • Comment on How do I access object subs from array?

Replies are listed 'Best First'.
Re: How do I access object subs from array?
by merlyn (Sage) on Oct 09, 2001 at 19:24 UTC
    Of course the above fails.
    It does? Have you tried it? It'll work just fine. Oh, you might need ()'s after $_, because some earlier Perls did require it (like 5.5.3, for example.)

    -- Randal L. Schwartz, Perl hacker

Re: How do I access object subs from array?
by Anonymous Monk on Oct 09, 2001 at 23:57 UTC
    Thank you very much for the lead.  The parens were what was
    causing the failure.