Obtain Filename and Lineno of the Function Caller

  • Thread starter Ilias Lazaridis
  • Start date
I

Ilias Lazaridis

sub title { my ($text) = @_;
print
"\n---------------------------------------------------------------------
\n";
warn "\===== DEMO: $text =====";
print
"---------------------------------------------------------------------
\n";
}

The above function simply prints a title with some formatting.

I 'abuse' the "warn" statement to print the filename and the line-
number.

problem: the line number and the file containing the 'sub title" is
printed.

need: I would need to print out the line number and filenname of the
function which CALLS "title".

can I achieve this in an automated way (e.g. no need to pass the
information to the "title" function)?

..
 
G

Gunnar Hjalmarsson

Ilias said:
sub title { my ($text) = @_;
print
"\n---------------------------------------------------------------------
\n";
warn "\===== DEMO: $text =====";
print
"---------------------------------------------------------------------
\n";
}

The above function simply prints a title with some formatting.

I 'abuse' the "warn" statement to print the filename and the line-
number.

problem: the line number and the file containing the 'sub title" is
printed.

need: I would need to print out the line number and filenname of the
function which CALLS "title".

can I achieve this in an automated way (e.g. no need to pass the
information to the "title" function)?

Try replacing

warn "\===== DEMO: $text =====";

with

my ($file, $line) = (caller)[1..2];
print "===== DEMO: $text ===== at $file line $line.\n";

perldoc -f caller
 
P

Paul Lalli

sub title { my ($text) = @_;
print
"\n---------------------------------------------------------------------
\n";
warn "\===== DEMO: $text =====";
print
"---------------------------------------------------------------------
\n";

}

The above function simply prints a title with some formatting.

I 'abuse' the "warn" statement to print the filename and the line-
number.

No need to abuse warn for that. You do realize that warn() prints to
STDERR while print() prints to STDOUT, right?
print "DEMO: $text. File " . __FILE__ . ", line: " . __LINE__;
problem: the line number and the file containing the 'sub title" is
printed.

need: I would need to print out the line number and filenname of
the function which CALLS "title".

Oooh, *so* close to a SAQ. :) Anyway, like Gunnar said, perldoc -f
caller.

However, if you later decide in a different program that you do want
this sort of statement to go to STDERR like your warn() does above,
see:
perldoc Carp

Paul Lalli
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top