How can I print source line number in Perl?

Viewed 36964

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

4 Answers

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...

Related