Skip to content

Commit 717715a

Browse files
committed
set magic on $lex for $lex = (index(...) == -1) and make it an lvalue
related to #17737 and fixes #17739
1 parent abe10ea commit 717715a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3522,9 +3522,11 @@ PP(pp_index)
35223522
if (PL_op->op_private & OPpTRUEBOOL) {
35233523
PUSHs( ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
35243524
? &PL_sv_yes : &PL_sv_no);
3525-
if (PL_op->op_private & OPpTARGET_MY)
3525+
if (PL_op->op_private & OPpTARGET_MY) {
35263526
/* $lex = (index() == -1) */
35273527
sv_setsv(TARG, TOPs);
3528+
SETTARG;
3529+
}
35283530
}
35293531
else
35303532
PUSHi(retval);

t/op/index.t

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BEGIN {
88
}
99

1010
use strict;
11-
plan( tests => 412 );
11+
plan( tests => 414 );
1212

1313
run_tests() unless caller;
1414

@@ -332,4 +332,30 @@ sub run_tests {
332332

333333
}
334334

335+
{
336+
my $store = 100;
337+
package MyTie {
338+
require Tie::Scalar;
339+
our @ISA = qw(Tie::StdScalar);
340+
sub STORE {
341+
my ($self, $value) = @_;
342+
343+
$store = $value;
344+
}
345+
};
346+
my $x;
347+
tie $x, "MyTie";
348+
$x = (index("foo", "o") == -1);
349+
ok(!$store, 'magic called on $lexical = (index(...) == -1)');
350+
}
351+
{
352+
is(eval <<'EOS', "a", 'optimized $lex = (index(...) == -1) is an lvalue');
353+
my $y = "foo";
354+
my $z = "o";
355+
my $x;
356+
($x = (index($y, $z) == -1)) =~ s/^/a/;
357+
$x;
358+
EOS
359+
}
360+
335361
} # end of sub run_tests

0 commit comments

Comments
 (0)