in reply to How can I call a Perl sub with an object from a sub name stored in a var?

Perhaps it would help if you showed the implementation code, instead of just a single line, because it works fine for me:

package Foo; use warnings; use strict; sub new { return bless {}, $_[0]; } sub blah { my ($self, $args) = @_; my $method = 'process'; my $updated = $self->$method($args); return $updated; } sub process { my ($self, $args) = @_; my @updated; push @updated, $_ * 2 for @$args; return \@updated; } package main; my $obj = Foo->new; my $method = 'blah'; my $args = [qw(1 2 3)]; my $return = $obj->$method($args); print "$_\n" for @$return;

Output

spek@scelia ~/scratch $ perl obj.pl 2 4 6

In my test suites, I very often put package and method names within variables to shorten the amount I have to type, as in each test file, I have a block for each group of tests, and there can be hundreds of tests, so instead of dozens of:

# test A { my $object = Some::Package::Name->new; is $object->some_method_name($y), 1, "some_method_name() with arg +$y ok"; ... } # test B { my $object = Some::Package::Name->new; is $object->some_method_name($z), 2, "some_method_name() with arg +$z ok"; ... } ...

I do this:

... my $mod = 'Some::Package::Name'; my $m = 'method_tested_in_this_test_file'; # testA { my $o = $mod->new; is $o->$m($arg), 1, "return val of $m with arg $arg ok"; ... } # testB { my $o = $mod->new; is eval {$o->$m('blah'); 1}, undef, "$m croaks if sent in 'blah'"; like $@, qr/idiot!/, "...and error message is sane"; ... } ...

And so on and so forth.

Update: Modified example to show method name as string within the object ($self) as well as outside of it.

Replies are listed 'Best First'.
Re^2: How can I call a Perl sub with an object from a sub name stored in a var?
by misterperl (Friar) on Dec 08, 2020 at 18:00 UTC
    Oh my - I guess I way-over-complicated this. All I need is:

    my $return = $obj->$method($args);

    I didn't know the method could be a variable.

    TY Vote ++ for all!

      One thing I forgot to mention is that the variable that encompasses the name of the subroutine should somehow reflect which sub it represents.

      Doing something like the following in a file with hundreds or thousands of lines of code can be frustratingly confusing:

      my $method_1 = 'twist_rubber_arm'; my $var_6 = 'choke_chicken'; my $x = 'let_cat_out_of_bag'; # far, far later on in the code say $object->$var_6('tomorrow');

      What the hell is $var_6 again?!? Time to go wading through code. Even with a good editor/IDE, backtracking is a time sink. Best to use names that are clearly representative in some way (as with all variables regardless if they map to a subroutine or not).

      $object->$method( @args );

      $method can be a string (method name) or a coderef.

      It can also be an overloaded object though, in which case it is stringified, not coderefified.

        Scratch head ... ?

        > $method ... can also be an overloaded object though,

        Is it really possible to overload the arrow operator -> for method calls from the RHS???

        I only see dereferencing listed ${} @{} %{} &{} *{}

        with_assign => '+ - * / % ** << >> x .', assign => '+= -= *= /= %= **= <<= >>= x= .=', num_comparison => '< <= > >= == !=', '3way_comparison'=> '<=> cmp', str_comparison => 'lt le gt ge eq ne', binary => '& &= | |= ^ ^= &. &.= |. |.= ^. ^.=', unary => 'neg ! ~ ~.', mutators => '++ --', func => 'atan2 cos sin exp abs log sqrt int', conversion => 'bool "" 0+ qr', iterators => '<>', filetest => '-X', dereferencing => '${} @{} %{} &{} *{}', matching => '~~', special => 'nomethod fallback ='

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        coderefified

        Did this word exist before Dec 09, 2020 at 17:58 CET? Anyway, now it does.

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      Yep, sometimes us programmers tend to think that the easy way must be wrong because it's too easy ;)