in reply to Calling func named in string
I'm answering my own question, I got it figured out - I was making this much harder than necessary. In the following, I included line 16 which calls outside of the "no strict 'refs'", causing an error, so as to show that "strict refs" is operating normally outside of the block. Also, if you only want to call out a sub by a number, you don't need the JumpTbl hash, that's only needed to call it out by name:
use strict; + use warnings; my @FuncList = qw(TestSub1 TestSub2); my %JumpTbl = map { $_ => $_ } @FuncList; { no strict 'refs'; $JumpTbl{'TestSub2'}(); $FuncList[0](); } $JumpTbl{'TestSub1'}(); exit; sub TestSub1 { print "TestSub1\n"; } sub TestSub2 { print "TestSub2\n"; }
Output:
TestSub2 TestSub1 Can't use string ("TestSub1") as a subroutine ref while "strict refs" +in use at ./triv.pl line 16.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Calling func named in string
by GrandFather (Saint) on Dec 15, 2024 at 20:29 UTC | |
by MikeL (Acolyte) on Dec 16, 2024 at 19:29 UTC | |
by GrandFather (Saint) on Dec 16, 2024 at 22:32 UTC |