Closed p5pRT closed 20 years ago
(my($foo) = $bar) =~ tr/ //d; # wrong answer
and
(my $foo = $bar) =~ tr/ //d; # correct answer
show different results. Since $bar is a scalar variable\, there's no ambiguity whether the context is a scalar or a list\, so the two results should be the same. Is it a bug?
On Feb 10\, Inyeol Lee said:
generated with the help of perlbug 1.26 running under perl 5.00503.
(my($foo) = $bar) =~ tr/ //d; # wrong answer (my $foo = $bar) =~ tr/ //d; # correct answer
(these test are in Perl 5.005_02)
This is independent of my:
$b = "a b c"; (($a) = $b) =~ tr/ //d; print $a; # a b c ($a = $b) =~ tr/ //d; print $a; # abc
$a = "a b c"; [$a]->[0] =~ tr/ //d; print $a; # a b c
Notice this error message though:
$a = "a b c"; ($a)[0] =~ tr/ //d; print $a; # ERROR: # Can't modify list slice in character translation near "tr/ //d;"
$a = "a b c"; @{[ $a ]}[0] =~ tr/ //d; print $a; # a b c
Sigh... the left-hand side is being VERY picky.
-- MIDN 4/C PINYAN\, NROTCURPI\, US Naval Reserve japhy@pobox.com http://www.pobox.com/~japhy/ http://pinyaj.stu.rpi.edu/ PerlMonth - An Online Perl Magazine http://www.perlmonth.com/ The Perl Archive - Articles\, Forums\, etc. http://www.perlarchive.com/
japhy@pobox.com writes: : On Feb 10\, Inyeol Lee said: : : >generated with the help of perlbug 1.26 running under perl 5.00503. : : >(my($foo) = $bar) =~ tr/ //d; # wrong answer : >(my $foo = $bar) =~ tr/ //d; # correct answer : : (these test are in Perl 5.005_02) : : This is independent of my: : : $b = "a b c"; : (($a) = $b) =~ tr/ //d; print $a; # a b c
That's a list assignment in scalar context\, so it produces the count of arguments on its right side\, which is 1\, which you then apply the tr/// to. So what it does is expected (by me :-)\, whether or not the my is there.
: ($a = $b) =~ tr/ //d; print $a; # abc
Also expected.
: $a = "a b c"; : [$a]->[0] =~ tr/ //d; print $a; # a b c
The list composer [$a] makes a copy of $a\, which the tr/// successfully changes\, rather than $a.
: Notice this error message though: : : $a = "a b c"; : ($a)[0] =~ tr/ //d; print $a; # ERROR: : # Can't modify list slice in character translation near "tr/ //d;"
That could possibly be made to work\, but the demand for fancy lvalues is never very high.
: $a = "a b c"; : @{[ $a ]}[0] =~ tr/ //d; print $a; # a b c
Again\, your list composer makes a copy of $a.
: Sigh... the left-hand side is being VERY picky.
Only with the list slice. The rest are working as designed.
Larry
Migrated from rt.perl.org#2136 (status was 'resolved')
Searchable as RT2136$