开发者

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

开发者 https://www.devze.com 2023-03-14 03:19 出处:网络
Perl usually complains about the line with the actual error, e.g. when a variable is used only once: use warnings;

Perl usually complains about the line with the actual error, e.g. 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 on use of 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 would this behaviour be useful?


perldoc perl5101delta:

The line numbers for warnings inside elsif are now correct.

Note that this change only affects elsif; you will still see runtime errors/warnings give the beginning or ending line number of the statement instead of the actual line of the offending 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


Some warnings are raised during parsing, some during execution. During parsing Perl knows exactly which line its on so can get the line number right. During execution, Perl doesn't really have the source code anymore, but does have the generated optree, which has tags to let it know which line it came from, except there may have been optimisations or simplifications that caused this information to be slightly off.

0

精彩评论

暂无评论...
验证码 换一张
取 消