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

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

Replies are listed 'Best First'.
Re^5: How to call subroutines using variables ?
by chromatic (Archbishop) on Jun 10, 2007 at 04:23 UTC

    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

        Why would you need to disable strict for this block:

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

        Without seeing the rest of the code, I can't tell you exactly what this does:

        my @val  = (\val_input,\val_input,\val_input,\val_input,\val_number,\val_date,\val_number,\val_filter);

        I can tell you that it doesn't take references to val_input, val_number, and so on. If you've predeclared those subroutines, Perl will call them and then take references to their return values. Otherwise, they're bare words, and strict will complain.

Re^5: How to call subroutines using variables ?
by blazar (Canon) on Jun 10, 2007 at 07:32 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);

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