in reply to How to sovle this, if i use strict i'm getting error.
Using a string as a subroutine name is almost always a bad idea. What you want instead is a "dispatch table", that is, a hash that maps your names to subroutines:
#!/usr/local/bin/perl -w use strict; sub test1 { print 'test1'; } sub test2 { print 'test2'; } for my $i (1,2) { my $name = "test$i"; if (exists $handler{$name}) { $handler{$name}->(); } else { warn "Don't know what to do with '$name'"; }; }; my %handler = ( test1 => \&test1, test2 => \&test2, );
This method allows you to extend your program while also giving you good error handling in the case that you want to call something by name which doesn't exist. It also makes your program more secure because only things can be called that you have specified as callable.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to sovle this, if i use strict i'm getting error.
by BUU (Prior) on Dec 06, 2005 at 09:10 UTC | |
by Corion (Patriarch) on Dec 06, 2005 at 09:14 UTC | |
by VSarkiss (Monsignor) on Dec 06, 2005 at 15:34 UTC | |
by jhourcle (Prior) on Dec 06, 2005 at 09:26 UTC | |
by BUU (Prior) on Dec 06, 2005 at 09:32 UTC |
In Section
Seekers of Perl Wisdom