You can call a subroutine whose name is stored in an array element (or, indeed, any scalar variable) like this:
#!/usr/bin/perl #use strict; use warnings; sub test { print "pass\n"; } my @subs = ('test'); $subs[0]();
However, note that I had to comment out the "use strict" in my code. This is generally a sign that you are doing something slightly dangerous. In this case you are using a technique called a "symbolic reference" which is really not recommended. The recommended way to do this is to store a reference to the subroutine instead of the subroutine's name. That looks like this:
#!/usr/bin/perl use strict; use warnings; sub test { print "pass\n"; } my @subs = (\&test); $subs[0]->();
Notice that in this case we are able to uncomment "use strict".
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re: calling functions using array variables
by davorg
in thread calling methods using array variables
by rsennat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |