Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to sovle this, if i use strict i'm getting error.

by Samy_rio (Vicar)
on Dec 06, 2005 at 07:15 UTC ( #514354=note: print w/ replies, xml ) Need Help??


in reply to How to sovle this, if i use strict i'm getting error.

Hi sen, Try this,

#!/usr/local/bin/perl use strict; for(my $i=1; $i<=2; $i++) { my $a = 'test'.$i; eval "&$a"; } sub test1 { print 'test1'; } sub test2 { print 'test2'; }

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';


Comment on Re: How to sovle this, if i use strict i'm getting error.
Select or Download Code
Re^2: How to sovle this, if i use strict i'm getting error.
by blazar (Canon) on Dec 06, 2005 at 12:02 UTC
    To sum up all that has been said, especially the warning re avoiding symrefs if possible, and here it is not only possible but also highly recommendable, so I'm stressing the point once more:
    #!/usr/bin/perl -l use strict; use warnings; my %dispatch=( test1 => sub { print 'test1'; }, test2 => sub { print 'test2'; } ); for(1,2) { $dispatch{"test$_"}->(); } __END__
    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:
    $dispatch{"test$_"}->() for 1,2;
    But then, if you just need a few numbered subs, depending on your actual application, you may prefer an array instead:
    #!/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__

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://514354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-12-18 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls