in reply to Invoking a method whose name is computed
Is there a more straightforward version?
I don't know if you will consider it more straightforward, but the can() universal method can be used here (see The UNIVERSAL Class in perlobj). As the original example is a class method call, argument passing is, well, straightforward. Had it been an object method, the object reference would need to be passed explicitly as the first argument. (Update: See the reply of afoken for the correct way to invoke with class name or object reference.) Also be aware that if the method does not exist, can() returns undef.
>perl -wMstrict -le "use 5.014; ;; package Foo { sub bar_baz { print 'i am bar_bazing! ', @_; } } ;; my $x = 'baz'; Foo->can(qq{bar_$x})->('Hooray!'); " i am bar_bazing! Hooray!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Invoking a method whose name is computed
by afoken (Chancellor) on Oct 02, 2012 at 05:43 UTC | |
by GrandFather (Saint) on Oct 02, 2012 at 20:39 UTC | |
Re^2: Invoking a method whose name is computed
by tobyink (Canon) on Oct 02, 2012 at 16:30 UTC |
In Section
Seekers of Perl Wisdom