http://www.perlmonks.org?node_id=1070664


in reply to Re: Invoking a string reference to a anonymous subroutine
in thread Invoking a string reference to a anonymous subroutine

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?

Replies are listed 'Best First'.
Re^3: Invoking a string reference to a anonymous subroutine
by LanX (Saint) on Jan 15, 2014 at 11:24 UTC
    > 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
        Ugh... :-|

        Look now this really doesn't make much sense anymore.

        I was under the impression that you also need to call the handlers manually as private methods??? (they are now restricted to the sub)

        If not _please_ use dispatch tables!

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        update

        older discussions covering dispatch tables (google ranked)