Can I access the line number that Perl is executing?

U

usenet

If I "die" a script, Perl tells me which line number it died on. So, I
presume, Perl is keeping track of that somehow.

I'm developing a script that is logging diagnostic messages. I would
like to include the line number that the message was logged from. I
thought I might find a "special character" such as $. that stores the
current line being executed... but, alas, I didn't spot such a special
character in my quickref.

Perl seems to "know" what line it's processing (because of the behavior
of 'die'). Can I access this bit of information?
 
A

Anno Siegel

If I "die" a script, Perl tells me which line number it died on. So, I
presume, Perl is keeping track of that somehow.

I'm developing a script that is logging diagnostic messages. I would
like to include the line number that the message was logged from. I
thought I might find a "special character" such as $. that stores the
current line being executed... but, alas, I didn't spot such a special
character in my quickref.

Perl seems to "know" what line it's processing (because of the behavior
of 'die'). Can I access this bit of information?

print "We're at ", __FILE__, ' line ', __LINE__, "\n";

Documented in perldata.

Anno
 
P

Paul Lalli

If I "die" a script, Perl tells me which line number it died on. So, I
presume, Perl is keeping track of that somehow.

I'm developing a script that is logging diagnostic messages. I would
like to include the line number that the message was logged from. I
thought I might find a "special character" such as $. that stores the
current line being executed... but, alas, I didn't spot such a special
character in my quickref.

Perl seems to "know" what line it's processing (because of the behavior
of 'die'). Can I access this bit of information?

In addition to Anno's tips, you might do well to keep in mind that,
like die(), warn() will also report the file and line number (provided
you don't end its argument list with a newline):

warn "Diag message: Count = $count";

will print to STDERR: "Diag message: Count = 42 at file.pl line 28"

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top