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