Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting current line number

Status
Not open for further replies.

dwhalen

Programmer
Feb 22, 2002
105
CA
Hey,

I am trying to write personal error_log for a perl program I wrote. What I want in the error_log is the line number where the error occured, the file name and the error message. I know how to get the file name ($0), and I will send my own text message, but is there anyway to get what line you are actually at in a perl program? Just to give you an idea of what I mean:
Code:
if($username !="something")
{
my $line = ????;
my $msg ="Your username does not equal something!!";
logError($0,$line,$msg);

}

Thanks

Later
 
You should look at Log4Perl.

<<neatly sidesteps the actual question>>

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 

Yeah, I thought the same thing but that gives you the line number of the current opened file handle....close but no cigar :)

Thanks

Later
 
You can get the current line number in your Perl program (and the filename) from the caller function. Here's an example:
Code:
#!perl
use strict;
use warnings;

my ($filename, $line) = (&who)[1,2];
print qq(\$filename = ${filename}\n\$line = ${line}\n);

sub who {caller}

Output:
$filename = caller.pl
$line = 5
Line 5 is the line in the program where the "who" sub was called, which invokes caller.
 
Code:
warn "error here";

$@ will also alter the error message


Kind Regards
Duncan
 
Hey,

Sorry I didn't respond sooner but I took yesterday off :).

The caller thing works great! Thanks for the help everyone.

Later
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top