in reply to Invoking a string reference to a anonymous subroutine

I prefer dispatch tables since they allow full control. Otherwise use eval to handle lexical symbolic references. The third solution shows a way to note it in one line. HTH! =)
use warnings; use strict; my $_parse_element_configuration = sub{ print "element_configuration: @_\n" }; my $handler = "_parse_element_configuration"; my $self=42; #--- dispatch table my %parser; $parser{_parse_element_configuration}=$_parse_element_configuration; my $meth=$parser{$handler}; $self->$meth(666); #--- lex sym ref via eval $meth= eval "\$$handler"; $self->$meth(777); #--- abstracted sub handle { my $name=shift; return eval "\$$name"; } handle($handler)->($self,888);
out:
element_configuration: 42 666 element_configuration: 42 777 element_configuration: 42 888

Cheers Rolf

( addicted to the Perl Programming Language)

update

corrected c&p problem but created duplicate! :-(

Replies are listed 'Best First'.
Re^2: Invoking a string reference to a anonymous subroutine
by AnomalousMonk (Archbishop) on Jan 14, 2014 at 22:43 UTC
    #--- dispatch table ... $self->$meth(666); ... #--- lex sym ref via eval ... $self->$meth(777);

    Don't both of these approaches take the interpreter on a useless (if there is no '42' package/class) or potentially bugilicious (if there is such a package and it contains a  _whatever method) run time search through the  @ISA tree? Why use the  -> operator to pass an ordinary (i.e., non-class/object reference) parameter? Only
        #--- abstracted
        ...
        handle($handler)->($self,888);
    avoids this possibly lengthy detour, but substitutes eval work at runtime.

    Of course, I'm not sure how one would create a '42' package in the first place, but what if it had been  $self = 'Foo'; in your example?

        I see: the trick is the RHS of the  -> must be a code reference. Hmmm... I dunno...

Re^2: Invoking a string reference to a anonymous subroutine
by Superfox il Volpone (Sexton) on Jan 15, 2014 at 11:15 UTC
    Hi there,
    thanks for your replies.

    I have tried the third approach by Lanx, but the program returns the following error:
    Variable "$_parse_element_configuration" is not available at (eval 8) +line 2. Use of uninitialized value in subroutine entry at Configuration.pm lin +e 64.
    My code is :
    my $_parse_element_configuration = sub{ ... } sub _handler{ my $function_name = shift; eval "\$$function_name"; }; [...] # invoking the handler _handler("_parse_element_configuration")->($self, $element);
    Where is the problem?

    Kind regards,
    s.fox

    p.s. my Perl version is 5.10, could it be involved?
      > Where is the problem?

      My code worked and the code you are showing now will also work.

      But maybe you should care to define _handler within the scope of your private $_parse... variables to have a proper closure?

      Otherwise Variable "$_parse_element_configuration" is not available at (eval 8) line 2."

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Hi Rolf,

        thanks for your reply.

        But maybe you should care to define _handler within the scope of your private $_parse... variables to have a proper closure?
        I believe you are right. I have come up with the following code:
        my $_parse_handler = sub { my $self = shift; my $function = shift . '0'; # installed handlers my $_parse_element_configuration0 = $_parse_element_configuration; my $_parse_element_machines0 = $_parse_element_machines; eval "\$$function"; }; [...] my $handler = $self->$_parse_handler("_parse_element_" . $element->nam +e() ); $self->$handler($element);
        Anyway I am getting the impression I am trying to bend the language to something it was not designed to do.

        Kind regards,
        s.fox