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

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!

  • Comment on Re^2: How can I call a Perl sub with an object from a sub name stored in a var?
  • Download Code

Replies are listed 'Best First'.
Re^3: How can I call a Perl sub with an object from a sub name stored in a var?
by stevieb (Canon) on Dec 08, 2020 at 18:51 UTC

    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).

Re^3: How can I call a Perl sub with an object from a sub name stored in a var?
by tobyink (Canon) on Dec 09, 2020 at 16:58 UTC
    $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

        As I said, the string overload is used.

        use strict; use warnings; use feature 'say'; BEGIN { package Local::Farm; sub new { my ( $class ) = ( shift ); bless {}, $class; } sub cow_say { say "Moo"; } sub sheep_say { say "Baa"; } sub horse_say { say "Neigh"; } }; BEGIN { package Local::Random::String; use overload q[""] => 'to_string', fallback => 1; sub new { my ( $class, @options ) = ( shift, @_ ); bless \@options, $class; } sub to_string { my ( $self ) = ( shift ); my $ix = rand @$self; $self->[$ix]; } }; my $object = 'Local::Farm'->new(); my $method = 'Local::Random::String'->new( qw/ cow_say sheep_say / ); $object->$method for 1 .. 10;
      coderefified

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

      Greetings,
      -jo

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

        Codified already has an established meaning which differs from what I meant.

Re^3: How can I call a Perl sub with an object from a sub name stored in a var?
by stevieb (Canon) on Dec 08, 2020 at 18:31 UTC

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