Absolut Newbie wrote:
> I have a program that generates a random number between 1 and 6.
> based on the outcome I want to run a subroutine that corresponds to
> the result. i.e if the result is 1 then the program should run sub
> F1. my question is how can I dynamically call the subroutine.
>
> i tried this but obviously it didn't work
>
> $var1 = int(rand(6)) + 1 ; # i didn't want zero as an answer
> $var1 ='F'.$var1; # sub names can't start w/ numbers
>
> $var1(); # this is where it crashes
You need &$var1();
But 'use strict' doesn't like that, because $var1 is a so-called "soft"
reference, so you can enclose it in a block like this:
{ no strict 'refs'; &$var1(); }
This is generally frowned upon, but for what you're doing it's fine.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>