in reply to Strange modification of @_
my ($str) = @_ does make a copy. Your complaint about uninitialized values isn't coming from the value of $str but rather from the fact that s/(x)// sets $1 to undefined. In fact s/(x)// doesn't even set $str to undef. It sets it to the empty string.
Perl subs pass by reference. Since fn $1 passes $1 as a parameter $_[0] and $1 share the same memory address. The error you are seeing is because print "@_" is equivalent to print "$_[0]" which is equivalent to print "$1" and $1 is undefined. I've modified your code sample a bit to make what is going on clearer:
sub fn { my ($str) = @_; my $rStr = \$str; my $rParam0 = \$_[0]; #print "params=<@_> param0=$rParam0 <$str> copy=$rStr\n"; printf "\\\$1=<%s> \\\$_[0]=<%s> str=<%s> \$1=<%s>\n" , \$1, \$_[0] , (defined($str) ? $str : 'undef') , (defined($1) ? $1 : 'undef'); $str =~ s/x//; printf "\\\$1=<%s> \\\$_[0]=<%s> str=<%s> \$1=<%s>\n" , \$1, \$_[0] , (defined($str) ? $str : 'undef') , (defined($1) ? $1 : 'undef'); } $_ = "x"; s/(x)/fn $1/e;
outputs
\$1=<SCALAR(0x818ae08)> \$_[0]=<SCALAR(0x818ae08)> str=<x> $1=<x> \$1=<SCALAR(0x818ae08)> \$_[0]=<SCALAR(0x818ae08)> str=<> $1=<undef>
Best, beth
|
|---|