develooper Front page | perl.perl5.porters | Postings from November 2012

[perl #115834] Successfull match $_[0] =~ /foo(.+)/ overrides $_[0] if function called as foo($1)

From:
Ed Avis via RT
Date:
November 21, 2012 19:33
Subject:
[perl #115834] Successfull match $_[0] =~ /foo(.+)/ overrides $_[0] if function called as foo($1)
Message ID:
rt-3.6.HEAD-17500-1353498943-1845.115834-15-0@perl.org
On Tue Nov 20 16:26:18 2012, doy@tozt.net wrote:

>The behavior here is the same behavior as

I believe it's not the same behaviour as the example you gave.
Expanding the code with one more final 'say':

    my $a;
    sub foo {
        say $_[0];
        $a = "bar";
        say $_[0];
    }
    $a = "foo";
    foo($a);
    say $a;

This prints foo bar bar.  But the analogous code using $1

    sub foo {
        say $_[0];
        'bar' =~ /(bar)/;
        say $_[0];
    }
    'foo' =~ /(foo)/;
    foo($1);
    say $1;

prints foo bar foo.  So while in the case of a global variable you set
the value seen by the caller - which makes sense - here $1 is aliased
enough to cause weird effects inside the subroutine, yet somehow local
enough for these effects to disappear when the subroutine exits.  This
halfway house is what causes the confusion IMHO.



---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=115834



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About