Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can I access the line number that Perl is executing?

Reply
Thread Tools

Can I access the line number that Perl is executing?

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      09-01-2005
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?

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-01-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-02-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      09-02-2005
(E-Mail Removed) wrote:

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


See caller():

<http://perldoc.perl.org/functions/caller.html>

You can see what line (and package, and file) a sub is called from. That
sub can be your diagnostic reporter sub.

--
Bart.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How can I access after an exception (in the rescue area) the file,class, method, line number for where the error occurred? Greg Hauptmann Ruby 4 01-11-2009 08:38 PM
install_driver(Oracle) failed: Can't load 'C:/Perl/site/lib/auto/DBD/Oracle/Oracle.dll' for module DBD::Oracle: load_file:The specified procedure could not be found at C:/Perl/lib/DynaLoader.pm line 230. Feyruz Perl Misc 4 10-14-2005 06:47 PM
Can I run perl under windows line by line? wld Perl Misc 4 02-12-2005 08:36 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments