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|