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

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.

Replies are listed 'Best First'.
Re^4: How to call subroutines using variables ?
by bradcathey (Prior) on Jun 09, 2007 at 19:48 UTC

    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.

        Obviously I'm missing something key here. I have:

        my @val = (\val_input,\val_input,\val_input,\val_input,\val_number,\v +al_date,\val_number,\val_filter); for my $i ( 0 .. $#list) { $code = $val[$i]; #which subroutine to call? ($sql{ $list[$i] }, $error) = $self->$code( $mand[$i], $self->query +->param($list[$i] )); if ( $error-> { msg } ) { push @errors, ucfirst($list[$i])." $error +->{ msg }" } }

        Sounds like I need a no strict 'refs' block in there. Possibly:

        } no strict 'refs'; $code = $val[$i]; }

        But now I'm just speculating.


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

      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.