views:

1855

answers:

3

Is it possible to get the current source line number in Perl? The equivalent in C++ is __LINE__.

+16  A: 
print "File: ", __FILE__, " Line: ", __LINE__, "\n";

or

warn("foo");
eed3si9n
warn will print on STDERR.
mat
+4  A: 

The __LINE__ literal is documented in the Special Literals section of the perldata man page.

converter42
+2  A: 

Note there's a gotcha with

perl -e'warn("foo")'

foo at -e line 1.

if it ends with a newline it won't print the line number

perl -e'warn("foo\n")'

foo

This is documented in "perldoc -f die", but is perhaps easy to miss in the "perldoc -f warn" section's reference to die...

bigiain