Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How can I call subroutines in an array

by dvergin (Monsignor)
on Jun 10, 2003 at 04:55 UTC ( #264540=note: print w/ replies, xml ) Need Help??


in reply to How can I call subroutines in an array

How's this:

use strict; use warnings; sub a1 { print("a1\n"); } sub a2 { print("a2\n"); } my @procs = (\&a1, \&a2); foreach (@procs) { &$_; } # or for a more idiomatic approach # enter anonymous subs directly into the array: my @procs2 = (sub { print("this\n") }, sub { print("that\n") } ); foreach (@procs2) { $_->(); # alternate form to call subroutine ref }
All of which prints:

    a1
    a2
    this
    that

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


Comment on Re: How can I call subroutines in an array
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-10-02 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (56 votes), past polls