Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

warnings

perl

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?

like image 733
Tim Avatar asked Dec 27 '22 18:12

Tim


1 Answers

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
like image 94
ysth Avatar answered Apr 18 '23 23:04

ysth