in reply to How Do I Get the Line Number In My Error Message?

The special tokens __FILE__ and __LINE__ report the filename and line number of the current source file you're in. They do not interpolate into quoted strings, though.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
  • Comment on Re: How Do I Get the Line Number In My Error Message?

Replies are listed 'Best First'.
Re^2: How Do I Get the Line Number In My Error Message?
by gri6507 (Deacon) on Nov 03, 2005 at 13:28 UTC
    Along the same lines of questioning, I always wanted to know how to write the following DEBUG function:

    my $var = 123; DebugPrint("Var is $var.\n"); sub DebugPrint { my $msg = shift; print "Called from $CallingFunction(): $msg"; }

    Is it possible to know what function called the DebugPrint() function (in other words, how do you set $CallingFunction)? If so, how?

      If you want information about your caller, maybe ask Perl about it. The caller function gives you the name of your caller and the line number and file it came from, where applicable.

      sub DebugPrint { my $msg = shift; my ($package,$filename,$line,$subroutine) = caller(1); print "Called from $subroutine(): $msg"; }
      Here is what I do.
      sub whoami { my( $level ) = @_; $level = 1 unless defined $level; print "DEBUG( whoami ): ", (caller(1))[3], "\n" if $DEBUG > $leve +l; return; } sub actually_does_something { whoami(); my( $foo, $bar ) = @_; # Do something useful here }

      -- Argel

      my intention is to get the caller line number, how can I do that ?

        Read the docs for caller.

        ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i);

        You may also want to read "Plain Old Comments (Not!)" and the end of perlsyn -- you can manipulate the file and line numbers that Perl reports.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: How Do I Get the Line Number In My Error Message?
by azaria (Beadle) on Nov 03, 2005 at 13:26 UTC
    Does this token _FILE_ is the name of the program (=$0) ?
      If you only have one file, yes, $0 will refer to the same file as __FILE__.

      If your program has more than one file (for example, if your code is a perl module), __FILE__ is more useful; $0 will tell you the name of the file that perl started running, but __FILE__ will tell you the file where you code is currently running. If you use the commands "require", "do", "use", __FILE__ will keep track of where the code came from: it's very handy.

      Also, notice that it's __FILE__ (with two underscores before and after the word), not _FILE_ (with one underscore).

      You might want to look at the module called "Carp": if you say "use Carp"; and then use the function "carp" where you would use the function "warn", you'll get messages that include the current line number, all the functions called to reach the current function, with file names and line numbers.

      Good Luck! :-)
      --
      Ytrew