In
mysub(), aliases to the global variables
$1 and
$2 are held in
@_, but those globals don't stay set as you expected for very "long":
use warnings;
use Data::Dumper;
sub mysub {
warn Dumper \@_; # globals still set
die 'single word expected' if (my $name = shift) !~ /^\w+$/;
warn Dumper \@_; # you "lost" the globals (due to matching /^
+\w+$/)
die 'single word expected' if (my $value = shift) !~ /^\w+$/;
}
$str = "abc def";
$str =~ /(\w+)\s+(\w+)/;
mysub($1, $2);
__END__
$VAR1 = [
'abc',
'def'
];
$VAR1 = [
undef
];
Use of uninitialized value $value in pattern match (m//) at ./t.pl lin
+e 12.
single word expected at ./t.pl line 12.
Save off global variables, or avoid using them:
my ($name, $value) = ($str =~ /(\w+)\s+(\w+)/);
mysub($name, $value);