tj_thompson has asked for the wisdom of the Perl Monks concerning the following question:
Assuming I have an obj reference $obj, that has a method foo such that $obj->foo returns something, what's the easiest way to get the return value of $obj->foo if the method name is stored in a scalar $method?
You can do:
# want the result of $obj->foo stored in $return_value my $method = 'foo'; my $return_value; eval '$return_value = $obj->'.$method; # now $return_value has what you need
But this seems clumsy and I doubt it's the best way. What's the right way to do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to access an object method when method name is stored in scalar variable
by Eliya (Vicar) on Mar 15, 2011 at 21:18 UTC | |
by tj_thompson (Monk) on Mar 15, 2011 at 21:22 UTC | |
by Eliya (Vicar) on Mar 15, 2011 at 21:31 UTC | |
by AnomalousMonk (Archbishop) on Mar 16, 2011 at 01:02 UTC | |
by tj_thompson (Monk) on Mar 15, 2011 at 21:37 UTC | |
|
Re: How to access an object method when method name is stored in scalar variable
by locked_user sundialsvc4 (Abbot) on Mar 16, 2011 at 15:35 UTC |