Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Calling func named in string

by tybalt89 (Monsignor)
on Dec 15, 2024 at 23:18 UTC ( [id://11163192]=note: print w/replies, xml ) Need Help??


in reply to Calling func named in string

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163188 use warnings; use List::AllUtils qw( sample ); my @FuncList = qw(TestSub1 TestSub2); for ( 1 .. 10 ) { my $func = sample 1, @FuncList; main->$func; } sub TestSub1 { print "TestSub1\n"; } sub TestSub2 { print "TestSub2\n"; }

Outputs:

TestSub2 TestSub2 TestSub1 TestSub2 TestSub2 TestSub2 TestSub1 TestSub2 TestSub1 TestSub1

Replies are listed 'Best First'.
Re^2: Calling func named in string
by tybalt89 (Monsignor) on Dec 15, 2024 at 23:29 UTC

    Or as one-liner:

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163188 use warnings; use List::AllUtils qw( sample ); my @FuncList = qw(TestSub1 TestSub2); for ( 1 .. 3 ) { main->${\sample 1, @FuncList}; } sub TestSub1 { print "TestSub1\n"; } sub TestSub2 { print "TestSub2\n"; }

    Outputs:

    TestSub2 TestSub1 TestSub2

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-11-09 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (65 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.