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

> my usual approach is something like

 &$self->$mysub ( eval $myargs )

WHY? oO

What is the content of $self?

That's pretty cryptic code. By convention is $self an object (i.e. a blessed ref) ... not the name of a sub.

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

  • Comment on Re: 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^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 16:25 UTC
    the methods are written, expecting an object.....
      Define yourself a custom function to do your "approach".

      I called it cryptic, guess why...

      DB<170> sub cryptic { my ($func,$meth,$args) = @_ ; $func->()->$meth +(eval $args) } DB<171> x [$self,$mysub,$myargs] 0 ARRAY(0x3c46108) 0 'bla' 1 'test' 2 '$a,$b' DB<172> &$self->$mysub ( eval $myargs ) [bless({}, "TEMP"), 42, 666] DB<173> cryptic( 'bla' , 'test', '$a,$b' ) [bless({}, "TEMP"), 42, 666] DB<174>

      you'll probably need to deactivate strict inside.

      edit

      and caller will tell you the name of the package from where you called cryptic()

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