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

Fellow Monks --
In a nutshell this is what I would like todo: When I exit normally by issuing the command exit; instead of say, having the following line of code:
print "exit line 382\n"; exit;
Is there a way perhaps by means of some special variable or other process that can perl can tell me which line the program exited on without hard coding it into a preceding print statement?
I did a little digging in perl documentation for special variables (my first thought), but nothing immediately jumped out at me. I will dig more, however, in the meantime I humbly ask for the wisdom of my fellow monks who are more experienced than I in this matter.
As always, any help/insight that you can provide is greatly appreciated!

Replies are listed 'Best First'.
Re: Exit With Line Number
by Roy Johnson (Monsignor) on May 05, 2005 at 16:05 UTC
    The token __LINE__ will give you what you want. You can use it as your exit status or print it before exiting. die will also (by default) tell you what line it exited at, but you exit with an error code.

    Caution: Contents may have been coded under pressure.
Re: Exit With Line Number
by davido (Cardinal) on May 05, 2005 at 16:09 UTC

    Well, there is die, and also the __LINE__ identifier, or you may consider using the Carp module, for finer handling of error conditions.

    __LINE__ is an identifier that always evaluates to the current line number in the script. So "print __LINE__, "\n"; would print the line number where the print function occurs.

    I hope this helps.


    Dave

Re: Exit With Line Number
by inman (Curate) on May 05, 2005 at 16:18 UTC
    You can always use die with an optional message if you just want to end the program and report the line number.
    #! /usr/bin/perl -w use strict; use warnings; print "The end is nigh!\n"; die "Arrrrrgh!"; __END__ The end is nigh! Arrrrrgh! at C:\Daniel\perl\PerlMonks\death.pl line 8.
    This is the usual thing to do if you want to exit following a failed test. E.g.
    open FILE, "no_such_file" or die $!;
Re: Exit With Line Number
by shemp (Deacon) on May 05, 2005 at 17:08 UTC
    You can use the built-in function caller() to get this sort of information, and much more. Its great for simple debugging.

    From perldoc:
    caller EXPR caller Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or "eval" or "require", and the undefined value otherwise. In list context, returns:

    ($package, $filename, $line) = caller;
    With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one.
    ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i) +;
Re: Exit With Line Number
by thundergnat (Deacon) on May 05, 2005 at 17:27 UTC
    My personal favorite way to do this is to use "warn"

    warn 'Exit' and exit;

    Will produce:

    Exit at (whatever).pl line (whichever).

      The only time this would be preferable over die, is when you want to return a specific exit code. Otherside, it's just overly verbose.

        Mostly true, though it's also useful when working with Tk, where a simple die often produces warnings about improper program termination.
Re: Exit With Line Number
by salva (Canon) on May 05, 2005 at 18:51 UTC
    you can also redefine exit:
    sub wexit (;$) { my $status = shift; my ($package, $fn, $line) = caller; print STDERR "exit status $status at $fn line $line\n"; CORE::exit($status) } *CORE::GLOBAL::exit = \&wexit;
    though, I would use this solution only for code I could not change, never for new code!