Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Side effect of passing matched values by reference

by puterboy (Scribe)
on Feb 21, 2013 at 17:16 UTC ( #1019987=perlquestion: print w/replies, xml ) Need Help??
puterboy has asked for the wisdom of the Perl Monks concerning the following question:

Perhaps this is obvious, but the behavior of this short code snippet confused me (actually it was buried in more complicated code that wasn't working for me), until I realized the potential indirect consequences of passing by reference... I pass it along for interest since at least for me the behavior was non-obvious...
my $bar = "abc"; foo($bar); $bar =~ /(.*)/; my $val = $1; foo($val); foo($1); print "|$1|$bar|\n"; sub foo { print "$_[0]\n"; $_[0] =~ /(.)/; print "$_[0]\n\n"; }
The result is:
abc abc abc abc abc a |abc|abc|

Replies are listed 'Best First'.
Re: Side effect of passing matched values by reference
by choroba (Bishop) on Feb 21, 2013 at 17:22 UTC
    That is not a consequence of passing by reference, in fact. It is a consequence of aliasing. See perlsub for details. This would not happen if you use a different variable than @_:
    sub foo { my $x = $_[0]; print "$x\n"; $x =~ /(.)/; print "$x\n\n"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Of course - that was the solution I used. My point was just that it wasn't obvious (at least to me) that $1 acts like a global variable in that way...
Re: Side effect of passing matched values by reference
by LanX (Bishop) on Feb 21, 2013 at 17:47 UTC
    Please ...

    $1 is a global variable which should only be carefully used in local context.

    But your match within the sub changes this global variable and your alias in $_[0] reflects this.

    And after leaving the sub the implicit localization restores the old value of $1...

    Better stop messing with perl internals or just pass a copy of $1 or follow chorobas suggestion.

    Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019987]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-03-20 00:48 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.