Local ($?) In END blocks - Why is it important what value was assigned to it?

At work, we encountered an error when interacting with child processes in the object's destructor and, ultimately, traced it to $? the variable will be overwritten during wait calls. This happens after calling exit (), so $? additionally means that our return code to the operating system.

In particular, perldoc talked about this error:

Inside the END $ routine? contains the value that will be returned by exit (). Can you change $? in the END routine, change the exit status of your program.

We do not want this to happen, so we put local $?=$?; inside each END block. But now programs return the success of the OS, without actually coping with their task.

I managed to break it into two test programs. One that works as intended, and one that fails. This happens on both v5.8.8 and v5.10.1 for x86_64-linux-thread-multi

Program A: (returns 0 to the operating system)

 END{ local $?=$?; } exit(100); 

Program B: (returns 100 to the operating system)

 END{ local $?=$?>>8; } exit(100); 

Why does it matter what value was assigned to local $? in the final block?

+4
source share
1 answer

Looks like a bug in perl. $? Self-determination apparently violated $? in local :

 % perl -wle '$? = 123; print "before: $?"; local $? = $?; print "after: $?"' before: 123 after: 0 

But this version works fine:

 % perl -wle '$? = 123; print "before: $?"; local $? = $? + 0; print "after: $?"' before: 123 after: 123 

Pretty strange.

An error report has been sent .

+7
source

All Articles