Why does perl complain about different lines for different kinds of warnings?

Perl usually complains about a line with an actual error, for example. when a variable is used only once:

use warnings; if ( 0 ) { } elsif ( $test ) { } # line 3 # Name "main::test" used only once: possible typo at testt.pl line 3. 

This does not work for warnings about using uninitialized $_ :

 use warnings; if ( 0 ) { # line 2 } elsif ( chomp ) { } # Use of uninitialized value $_ in scalar chomp at testt.pl line 2. use warnings; if ( 0 ) { # line 2 } elsif ( m/test/ ) { } # Use of uninitialized value $_ in pattern match (m//) at testt.pl line 2. 

What causes this? When will this behavior be useful?

+4
source share
2 answers

perldoc perl5101delta :

Line numbers for alerts inside elsif are now correct.

Note that this change only affects elsif; you will still see errors / warnings at runtime, specify the line number of the beginning or end of the line instead of the actual line of the violating code:

 $ perl use warnings; 0 ? do { } : $test ? do { } : do { }; 0 ? do { } : chomp() ? do { } : do { }; Name "main::test" used only once: possible typo at - line 3. # correct Use of uninitialized value $_ in scalar chomp at - line 8. # incorrect 
+7
source

Some warnings occur during parsing, some at runtime. During parsing, Perl knows exactly which line on it can get the line number on the right. At run time, Perl doesn't actually have the source code, but has a generated optree that has tags to tell it which line it came from, with the possible exception of optimizations or simplifications that caused this information to slightly go away.

+3
source

All Articles