On Nov 9, 1:27 pm, [EMAIL PROTECTED] (Martijn) wrote:
> I simple question to which that I can't find the answer.
You should have tried the FAQ first.
> How does one
> create a subroutine whose name is defined by the value of a variable?
perldoc -q "variable name"
> To be more precise, I need to write a bunch of subroutines foo_bar1,
> foo_bar2, ...
No you don't. Naming your variables with numeric indices is a giant
red flag that you should be using an array instead. And yes,
subroutines are just another kind of variable in Perl, so the same
rule applies.
> where 1) I can't know the number of these routines
> beforehand and 2) because of other modules I am using, they need to
> have these very names. In particular, I can't make one subroutine
> foo_bar that has the number as its argument. Well, I can, but I'd
> still make my code understand that foo_bar1() should call foo_bar(1).
>
> I thought this was something quite simple, but now I'm not even sure
> if it's possible at all.
The answer to the question you asked is "use symrefs". The answer to
the question you SHOULD have asked is: use an array of subroutine
references.
my @foo_bar_subs = (
sub {
print "I'm the first sub!\n";
},
sub {
print "I'm number 2!!\n";
},
# . . .
);
#call the first foo_bar_sub:
$foo_bar_subs[0]->();
Paul Lalli
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/