On 2/28/07, Michael Gale <[EMAIL PROTECTED]> wrote:

        I am trying to do the following, do I need to remove the strict option 
for
this to work or is there a better way ?

There is a better way.

     my @actions = ( "ssh_login", "su_login", "su_logout", "ssh_logout", "cmds" 
);

     foreach my $a (@actions) {

        &show_log("Calling $a");
        my $check= &{$a};

Under "strict refs", this fails. One reason is that this code could,
potentially, call *any* subroutine available, and that's hard on
maintainability and security.

The goal, then, is to find a better way to start with a string and end
up with a subroutine call. Using a soft reference (i.e., a string) to
do it is forbidden by 'strict'. But a normal reference is fine.

 my %action_table = (
   ssh_login => \&ssh_login,
   su_login => \&su_login,
   ... # all the rest
 );

Now that you have that table, you can call the subroutine safely like this:

 my $check = &{ $action_table{$a} }();

Or, using the arrow notation:

 my $check = $action_table{$a}->();

Either way, you can be sure that no *other* subroutine will ever be
called, no matter what strings somehow get into $a. Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to