Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.94k stars 553 forks source link

split bitwise-assignment operators (`&=`, `&.=`, etc) don't return lvalues #22412

Closed mauke closed 2 months ago

mauke commented 3 months ago

Description Assignment operators (like = itself, but also += or >>=) are supposed to return their left operand as an lvalue, which can then be further modified within the same expression. The old combined numeric/string bitwise assignment operators do this, but the new(er) split numeric and string bitwise operators (as provided by use feature qw(bitwise)) do not.

Steps to Reproduce

$ perl -wle '$_ = 1; ($_ |= 9) ^= 4; print'
13
$ perl -wle '$_ = "HI"; ($_ |= "") ^= " " x 2; print'
hi
$ perl -wle 'use feature qw(bitwise); $_ = 1; ($_ |= 9) ^= 4; print'
Can't modify numeric bitwise or (|) in numeric bitwise xor (^) at -e line 1, near "4;"
Execution of -e aborted due to compilation errors.
$ perl -wle 'use feature qw(bitwise); $_ = "HI"; ($_ |.= "") ^.= " " x 2; print'
Can't modify string bitwise or (|.) in string bitwise xor (^.) at -e line 1, near "2;"
Execution of -e aborted due to compilation errors.

Expected behavior

$ perl -wle '$_ = 1; ($_ |= 9) ^= 4; print'
13
$ perl -wle '$_ = "HI"; ($_ |= "") ^= " " x 2; print'
hi
$ perl -wle 'use feature qw(bitwise); $_ = 1; ($_ |= 9) ^= 4; print'
13
$ perl -wle 'use feature qw(bitwise); $_ = "HI"; ($_ |.= "") ^.= " " x 2; print'
hi

v5.40.0

sisyphus commented 3 months ago

Heh ... the same behaviour apparently permeates through to the overloading of these operators. Perhaps that's an obvious ramification that didn't need to be pointed out. (I don't know.)

D:\>perl -MMath::BigInt -wle "$x = Math::BigInt->new(64); ($x &= 3) |= 2; print $x;"
2

D:\>perl -MMath::BigInt -wle "use feature qw(bitwise); $x = Math::BigInt->new(64); ($x &= 3) |= 2; print $x;"
Can't modify numeric bitwise and (&) in numeric bitwise or (|) at -e line 1, near "2;"
Execution of -e aborted due to compilation errors.