Can I access the line number that Perl is executing?

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Sep 1, 2005.

  1. Guest

    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?
     
    , Sep 1, 2005
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> 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.
     
    Anno Siegel, Sep 1, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    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
     
    Paul Lalli, Sep 2, 2005
    #3
  4. Bart Lateur Guest

    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.
     
    Bart Lateur, Sep 2, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,385
    Matt Humphrey
    Oct 18, 2004
  2. Dr. Phillip M. Feldman

    How to find number of line that is currently executing?

    Dr. Phillip M. Feldman, Oct 10, 2009, in forum: Python
    Replies:
    1
    Views:
    324
    ryles
    Oct 10, 2009
  3. Greg Hauptmann
    Replies:
    4
    Views:
    123
    Greg Hauptmann
    Jan 11, 2009
  4. Nick
    Replies:
    14
    Views:
    355
    Tad McClellan
    Oct 3, 2003
  5. wld
    Replies:
    4
    Views:
    169
    Jazeker
    Feb 12, 2005
Loading...

Share This Page