You are using what's known as a symbolic reference, or a soft reference. You really probably don't need or even want to do that. Here are a few alternatives to the same code:
#-------------------- # Option #1: use strict; my %lists = ( 'test1' => sub { print "New message for test1\n"; }, 'test2' => sub { print "New message for test2\n"; } ); for my $i ( keys %lists ) { $lists{$i}->(); } # ------------------- # Option #2: use strict; sub test1 { print "New message for test1\n"; } sub test2 { print "New message for test2\n"; } my %lists = ( 'test1' => \&test1, 'test2' => \&test2 ); foreach my $i ( keys %lists ) { $lists{$i}->(); }
This and more is covered in perlreftut and perlref. They're pretty good reads, and terribly helpful if you're trying to grapple with references. As for why using strict causes your code to fail, well, because that's exactly the kind of code strictures is supposed to cause to fail.
Hope this helps...
Dave
In reply to Re: Can't Reference a Sub by Variable when using Strict
by davido
in thread Can't Reference a Sub by Variable when using Strict
by Dru
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |