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


In reply to Re: Strange modification of @_ by ELISHEVA
in thread Strange modification of @_ by jrw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.