44import org .objectweb .asm .MethodVisitor ;
55import org .objectweb .asm .Opcodes ;
66import org .perlonjava .astnode .*;
7+ import org .perlonjava .perlmodule .Warnings ;
78import org .perlonjava .runtime .RuntimeContextType ;
89
910public class EmitForeach {
@@ -24,17 +25,26 @@ static void emitFor1(EmitterVisitor emitterVisitor, For1Node node) {
2425 opNode .operator .equals ("$" ) &&
2526 opNode .operand instanceof IdentifierNode idNode &&
2627 idNode .name .equals ("_" )) {
27- int varIndex = emitterVisitor .ctx .symbolTable .getVariableIndex ("$_" );
28- if (varIndex == -1 ) {
29- node .variable = new OperatorNode ("our" , node .variable , node .variable .getIndex ());
30- }
28+ node .variable = new OperatorNode ("our" , node .variable , node .variable .getIndex ());
3129 }
3230
3331 // First declare the variables if it's a my/our operator
3432 if (node .variable instanceof OperatorNode opNode &&
3533 (opNode .operator .equals ("my" ) || opNode .operator .equals ("our" ))) {
34+ boolean isWarningEnabled = Warnings .warningManager .isWarningEnabled ("redefine" );
35+ if (isWarningEnabled ) {
36+ // turn off "masks earlier declaration" warning
37+ Warnings .warningManager .setWarningState ("redefine" , false );
38+ }
39+ // emit the variable declarations
3640 node .variable .accept (emitterVisitor .with (RuntimeContextType .VOID ));
37- node .variable = ((OperatorNode ) node .variable ).operand ;
41+ // rewrite the variable node without the declaration
42+ node .variable = opNode .operand ;
43+
44+ if (isWarningEnabled ) {
45+ // restore warnings
46+ Warnings .warningManager .setWarningState ("redefine" , true );
47+ }
3848 }
3949
4050 // Obtain the iterator for the list
@@ -78,7 +88,7 @@ static void emitFor1(EmitterVisitor emitterVisitor, For1Node node) {
7888 // Assign to variable
7989 Node varNode = varList .elements .get (i );
8090 if (varNode instanceof OperatorNode operatorNode ) {
81- String varName = operatorNode .operator + ((IdentifierNode )operatorNode .operand ).name ;
91+ String varName = operatorNode .operator + ((IdentifierNode ) operatorNode .operand ).name ;
8292 int varIndex = emitterVisitor .ctx .symbolTable .getVariableIndex (varName );
8393 emitterVisitor .ctx .logDebug ("FOR1 multi var name:" + varName + " index:" + varIndex );
8494 mv .visitVarInsn (Opcodes .ASTORE , varIndex );
@@ -91,7 +101,7 @@ static void emitFor1(EmitterVisitor emitterVisitor, For1Node node) {
91101 mv .visitTypeInsn (Opcodes .CHECKCAST , "org/perlonjava/runtime/RuntimeScalar" );
92102
93103 if (node .variable instanceof OperatorNode operatorNode ) {
94- String varName = operatorNode .operator + ((IdentifierNode )operatorNode .operand ).name ;
104+ String varName = operatorNode .operator + ((IdentifierNode ) operatorNode .operand ).name ;
95105 int varIndex = emitterVisitor .ctx .symbolTable .getVariableIndex (varName );
96106 emitterVisitor .ctx .logDebug ("FOR1 single var name:" + varName + " index:" + varIndex );
97107 mv .visitVarInsn (Opcodes .ASTORE , varIndex );
0 commit comments