in reply to Re: How to call subroutines using variables ?
in thread How to call subroutines using variables ?

I tried:

my @all_subs = (\&sub_1,\&sub_2); $all_subs[1]->();

which worked fine. But in CGI::Application I'm using $self->, as in:

$self->$all_subs[1]->();

but got a syntax error (doesn't like the 2nd arrow operator). How can I get around this? Thanks.

Update: fixed some punctuation


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re^3: How to call subroutines using variables ?
by blazar (Canon) on Jun 09, 2007 at 17:56 UTC

    which worked fine. But in CGI::Application I'm using $self->, as in:

    $self->$all_subs[1]->();

    Use:

    my $code=$all_subs[1]; $self->$code();

    This will call $code as a method on $self, which AIUI is what you want. If you would like to do the same without an intermediate variable, then I wondered too and it turns out to be possible, albeit in a somewhat convoluted way.

      Worked great, but I did have to get rid of the & in front of my subroutine names. So:

      my @all_subs = (\sub_1,\sub_2);

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

        You're not using strict, are you? That ought to call those subroutines immediately and take a reference to their results.

        Worked great, but I did have to get rid of the & in front of my subroutine names. So:

        my @all_subs = (\sub_1,\sub_2);

        In which sense did you "have" to? That is something completely different.