in reply to Re: How to sovle this, if i use strict i'm getting error.
in thread How to sovle this, if i use strict i'm getting error.
Notice that I also used -> rather than & for dereferencing, which is preferrable for various reasons, and a (more) perlish for loop, which could have also been a modifier:#!/usr/bin/perl -l use strict; use warnings; my %dispatch=( test1 => sub { print 'test1'; }, test2 => sub { print 'test2'; } ); for(1,2) { $dispatch{"test$_"}->(); } __END__
But then, if you just need a few numbered subs, depending on your actual application, you may prefer an array instead:$dispatch{"test$_"}->() for 1,2;
#!/usr/bin/perl -l use strict; use warnings; my @test=( sub { print 'test0' }, sub { print 'test1' } ); $test[$_]->() for 0,1; # or even $_->() for @test; __END__
|
|---|