bprew has asked for the wisdom of the Perl Monks concerning the following question:

Hey,

I was wondering if there was a way to print the current line of the script when printing error messages. I currently use __LINE__.. but I was looking for something shorter. Can I override __DIE__ or warn and if so, how?

Thanks!
--
Ben
"Naked I came from my mother's womb, and naked I will depart."

Replies are listed 'Best First'.
(bbfu) Re: Lines in error messages
by bbfu (Curate) on Oct 25, 2002 at 02:28 UTC

    #!perl use warnings; use strict; sub line { return((caller())[2]); } print "Line is: ", line(), "\n";

    Note that it has to be in a sub. :)

    bbfu
    Black flowers blossum
    Fearless on my breath

Re: Lines in error messages
by PodMaster (Abbot) on Oct 25, 2002 at 02:44 UTC
    Can I override __DIE__ or warn and if so, how?
    What's that mean?

    I'm guessing you wanna read sigtrap and perlvar.

    You also might wanna check out Carp. When I'm doing any "debugging", i just use warn (or carp), cause it already prints the line number.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      Hehe, I actually poked around perlvar a little bit, but by over-riding __DIE__ I was referring to this node. I will definately finish looking through perlvar.

      Carp sounds like exactly what I'm looking for. I didn't realize Carp printed the line number automatically, so it should work almost exactly like what I was looking for.

      thanks
      --
      Ben
      "Naked I came from my mother's womb, and naked I will depart."