in reply to Error Message when I use Strict

another way to make this work under strict: Dispatch Tables.
use strict; use warnings; my $SubroutName='test'; my %SubroutDispatch = ( test => \&test, test1 => \&test1, test2 => \&test2, ); my $text='abcdefgijkl'; my $sub = $SubroutDispatch{$SubroutName}; &$sub($text); sub test { print "\nokey $_[0]!"; } sub test1{ print "\nokey!"; } sub test2{ print "\nokey!"; }


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Error Message when I use Strict
by Anonymous Monk on Jul 29, 2005 at 11:50 UTC
    What does that buy you over:
    $SubroutName = 'test'; { no strict 'refs'; &$SubroutName; }
    There's not much danger in using symbolic references - what "use strict 'refs'" does for you is prevent you from using symbolic references accidentally (which can be a problem), but that isn't the case here. The biggest danger lies probably in mistyping the value for $SubroutName, which will lead to a run-type error. But your dispatch table suffers from that as well. The use of a symbolic reference to call a sub hardly differs from your solution - except that the dispatch table isn't coded explicitly - you're using the stash as the dispatch table.
      The biggest danger lies probably in mistyping the value for $SubroutName, which will lead to a run-type error.

      No, the biggest danger lies in accidentally calling another subroutine that you don't plan on calling there. This becomes especially dangerous if the subname is passed from the user (like from a CGI form). Using a dispatch table makes the possible calls explicit, which is usually a good thing.

      But your dispatch table suffers from that as well.
      my $sub = $SubroutDispatch{$SubroutName} || sub {return};


      holli, /regexed monk/