1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl -w use strict; my $A = q(my $arg = shift;print "A ";print "$arg\n";); my $B = q(print "B\n";); my %subs = ( A => sub { $A }, B => sub { $B }, O => sub {my $arg = shift;print "O ";print "$arg\n";} ); for my $key (keys %subs ) { eval &{$subs{$key}}(10) ; }
1
2
3
4
5
A
Use of uninitialized value $arg in concatenation (.) or string at (eval 17)[F:\Perl-1.pl:13] line 1.
O 10
B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#! /usr/bin/perl use strict; use warnings; my $A = q(my $arg = shift;print "A ";print "$arg\n";); my $B = q(print "B\n";); my %subs = ( A => eval "sub { $A }", B => eval "sub { $B }", O => sub {my $arg = shift;print "O ";print "$arg\n";} ); for my $key (sort keys %subs ) { eval $subs{$key}->(10) ; }