From: Rafael Garcia-Suarez Date: Wed, 4 Feb 2004 19:50:05 +0000 (+0000) Subject: The "my $_" wasn't nice with foreach loops. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aabe9514ecdf64c885843e2a9440477e5a96d4c8;p=p5sagit%2Fp5-mst-13.2.git The "my $_" wasn't nice with foreach loops. p4raw-id: //depot/perl@22268 --- diff --git a/op.c b/op.c index 62d9b03..83c6fc1 100644 --- a/op.c +++ b/op.c @@ -3721,7 +3721,14 @@ Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *blo Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]); } else { - sv = newGVOP(OP_GV, 0, PL_defgv); + I32 offset = pad_findmy("$_"); + if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS(offset) & SVpad_OUR) { + sv = newGVOP(OP_GV, 0, PL_defgv); + } + else { + padoff = offset; + iterpflags = OPpLVAL_INTRO; /* my $_; for () */ + } } if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART); diff --git a/t/op/mydef.t b/t/op/mydef.t index 700a3dc..0770e78 100644 --- a/t/op/mydef.t +++ b/t/op/mydef.t @@ -5,7 +5,7 @@ BEGIN { @INC = '../lib'; } -print "1..56\n"; +print "1..64\n"; my $test = 0; sub ok ($$) { @@ -66,6 +66,23 @@ $_ = "global"; ok( our $_ eq 'global', '...our $_ restored outside for my $_' ); } { + my $_ = 'local'; + for ("implicit foo") { # implicit "my $_" + ok( $_ eq "implicit foo", 'for implicit my $_' ); + /(.)/; + ok( $1 eq "i", '...m// in for implicity my $_' ); + ok( our $_ eq 'global', '...our $_ inside for implicit my $_' ); + } + ok( $_ eq 'local', '...my $_ restored outside for implicit my $_' ); + ok( our $_ eq 'global', '...our $_ restored outside for implicit my $_' ); +} +{ + my $_ = 'local'; + ok( $_ eq "postfix foo", 'postfix for' ) for 'postfix foo'; + ok( $_ eq 'local', '...my $_ restored outside postfix for' ); + ok( our $_ eq 'global', '...our $_ restored outside postfix for' ); +} +{ for our $_ ("bar") { ok( $_ eq "bar", 'for our $_' ); /(.)/;