Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print source line number in Perl?

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

like image 565
David Sykes Avatar asked Dec 10 '08 08:12

David Sykes


People also ask

How do I get current line number in Perl?

Using the special Perl variable $! in the message passed to die. You display the current line of execution in a Perl script by referring to it with the __LINE__ token. You display the name of the current Perl package with __PACKAGE__. or die $!

How do I print a line in Perl?

The Perl print function\n"; Notice that you need to supply the newline character at the end of your string. If you don't supply that newline character, and print multiple lines, they'll all end up on one long line of output, like this: Hello, world.

What is $$ in Perl?

$$ - The process number of the Perl running this script. $0 - Contains the name of the program being executed.


2 Answers

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

print "File: ", __FILE__, " Line: ", __LINE__, "\n"; 

or

warn("foo"); 
like image 127
Eugene Yokota Avatar answered Sep 21 '22 21:09

Eugene Yokota


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

like image 23
bigiain Avatar answered Sep 21 '22 21:09

bigiain