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.
In reply to Re: How to sovle this, if i use strict i'm getting error.
by Corion
in thread How to sovle this, if i use strict i'm getting error.
by sen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |