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!
In reply to Re: Invoking a method whose name is computed
by AnomalousMonk
in thread Invoking a method whose name is computed
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |