Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Obtain Filename and Lineno of the Function Caller


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)?

.

--
http://dev.lazaridis.com/lang/wiki/Perl

Try replacing

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

with

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

perldoc -f caller

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

On Jun 6, 5:42 am, Ilias Lazaridis <i@lazaridis.com> wrote:

> 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

thanks for the concise replies.

everything works nice.

summarized here:

http://dev.lazaridis.com/lang/ticket/11

.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc