Skip to content

Commit

Permalink
Propagate lvalue context to && and ||
Browse files Browse the repository at this point in the history
I did this for the last statement in an lvalue sub (and the argument
to ‘return’ in an lvalue sub) in commit 2ec7f6f.

It turns out that it needs to apply in other lvaluish contexts, too:

$ ./perl -Ilib -le 'for($a||$b){$_++} print $b'
1
$ ./perl -Ilib -le 'for(pos $a || pos $b){$_++} print pos $b'
Modification of a read-only value attempted at -e line 1.

If I can assign to $b through this construct, then why not pos?
  • Loading branch information
Father Chrysostomos committed Oct 25, 2013
1 parent 447779a commit 2e73d70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 2 additions & 4 deletions op.c
Expand Up @@ -2298,10 +2298,8 @@ Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)

case OP_AND:
case OP_OR:
if (type == OP_LEAVESUBLV) {
op_lvalue(cLOGOPo->op_first, type);
op_lvalue(cLOGOPo->op_first->op_sibling, type);
}
op_lvalue(cLOGOPo->op_first, type);
op_lvalue(cLOGOPo->op_first->op_sibling, type);
goto nomod;
}

Expand Down
8 changes: 7 additions & 1 deletion t/op/or.t
Expand Up @@ -25,7 +25,7 @@ sub FETCH {
package main;
require './test.pl';

plan( tests => 8 );
plan( tests => 9 );


my ($a, $b, $c);
Expand Down Expand Up @@ -66,3 +66,9 @@ $c = $a || $b;
local $TODO = 'Double FETCH';
is($c, 1, ' $tied || $var');
}

$y = " ";
for (pos $x || pos $y) {
eval { $_++ };
}
is(pos($y) || $@, 1, "|| propagates lvaluish context");

0 comments on commit 2e73d70

Please sign in to comment.