develooper Front page | perl.perl5.porters | Postings from August 2013

[perl #54728] PathTools-3.27 triggers a bug in Perl

Thread Previous | Thread Next
From:
Victor Efimov via RT
Date:
August 4, 2013 18:13
Subject:
[perl #54728] PathTools-3.27 triggers a bug in Perl
Message ID:
rt-3.6.HEAD-2552-1375639999-1247.54728-15-0@perl.org
Let's try replace "$1" with $x in your example and explanation (and
localize it manually):

########
our $x;

$x = 2;
print test1("1", $x), "\n";

sub test1 {
local $x;
$x = 'a';
return $_[0].'/'.$_[1];
}
########

1) $x=2 sets $x to "2".
2) test1("1", $1) calls test1 with "1" and $x.
3) $x = 'a' sets $x to "a".
4) $_[0].'/'.$_[1] builds a string from the arguments passed to test1.
Perl passes arguments by alias.
$_[0] is an alias to "1".
$_[1] is an alias to $x, which currently contains "a".
5) Thus test1 returns "1/a".
6) $x is explicitly localized, so $x reverts to "2" here.
7) "1/a\n" is printed.

#########

However it's not true. This example with "$x" prints 1/2

Anyway, even if we find it's not a bug, it's unique situation and can be
documented (btw who is responsible for that kind of side effects -
caller or subroutine?)

On Sun Aug 04 06:52:21 2013, plokinom@gmail.com wrote:
> On 04.08.2013 15:12, Alexandr Ciornii wrote:
> > This ticket can be viewed as two tickets:
> > 1. Bug in PathTools (fixed)
> > 2. This problem:
> > #!perl
> >
> > "2" =~ m/(.*)/;
> > print test1("1", $1), "\n";
> >
> > sub test1 {
> >    'aa' =~ m/(.)/;
> >    return $_[0].'/'.$_[1];
> > }
> > prints "1/a" on perl 5.18.0.
> >
> > Can it be considered a bug?
> 
> I don't see it as a bug.
> 
> 1) "2" =~ m/(.*)/ sets $1 to "2".
> 2) test1("1", $1) calls test1 with "1" and $1.
> 3) 'aa' =~ m/(.)/ sets $1 to "a".
> 4) $_[0].'/'.$_[1] builds a string from the arguments passed to test1.
>     Perl passes arguments by alias.
>     $_[0] is an alias to "1".
>     $_[1] is an alias to $1, which currently contains "a".
> 5) Thus test1 returns "1/a".
> 6) Match variables are implicitly localized, so $1 reverts to "2" here.
> 7) "1/a\n" is printed.
> 




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

Thread Previous | Thread Next


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