For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > getting file name/line num info









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getting file name/line num info
Manish Sapariya

2005-04-25, 3:56 pm

Hello,
Can somebody explain me the magic here....

=============================
sub vprint (@) {
return unless $Utils::verbose;
my ($file, $line) = (caller)[1,2];
my $subr = (caller 1)[3] || 'main';
$file =~ s!.*/!!;
print STDERR "$subr($file:$line): ", @_;
}
================

What would be the better way to add facilty to
write to a log file as well.

- by passing a file handle (or some other mechanism).
Write to file handle only if one is passed to the
subroutine, otherwise just print to the STDERR.

Thanks and Regards,
Manish
Jay Savage

2005-04-25, 3:56 pm

On 4/25/05, Manish Sapariya <manishs@gs-lab.com> wrote:
> Hello,
> Can somebody explain me the magic here....
>=20


sub vprint (@) { =20
# subroutine declaration with prototype

return unless $Utils::verbose;
# exit sub unless $Utils::verbose flag is set

my ($file, $line) =3D (caller)[1,2];
# take a slice (the second and third items: the file containing
# the package where the subroutine was called, and the line
# number where it was callled) of the list returned by the=20
# caller() function, and assign the values to $file and $line. =
=20
# see perldoc -f caller for details.

my $subr =3D (caller 1)[3] || 'main';
# take a slice of (only the thrid item) of the list returned
# by caller(1) and assign the value (the subroutine name
# of the subroutine where the current subroutine (vprint)
# was invoked to $subr. (caller 1)[3] is undefined, use=20
# 'main' instead. see perldoc -f caller for details.

$file =3D~ s!.*/!!;
# strip everything up to the last '/' character from $file
# in case (caller)[1] is a full path name.

print STDERR "$subr($file:$line): ", @_;
# print the subroutine, file, and line number information,=20
# along with anything else passed to the sub on STDERR.
}

HTH,

--jay
Manish Sapariya

2005-04-26, 8:57 am

Thanks,
I did not realize that (caller) is a function call
and [1,2] was a slicing operation.

Thanks for the info.
Regards,
Manish


On 04/25/2005 08:49 PM, Jay Savage wrote:
> On 4/25/05, Manish Sapariya <manishs@gs-lab.com> wrote:
>
>
>
> sub vprint (@) {
> # subroutine declaration with prototype
>
> return unless $Utils::verbose;
> # exit sub unless $Utils::verbose flag is set
>
> my ($file, $line) = (caller)[1,2];
> # take a slice (the second and third items: the file containing
> # the package where the subroutine was called, and the line
> # number where it was callled) of the list returned by the
> # caller() function, and assign the values to $file and $line.
> # see perldoc -f caller for details.
>
> my $subr = (caller 1)[3] || 'main';
> # take a slice of (only the thrid item) of the list returned
> # by caller(1) and assign the value (the subroutine name
> # of the subroutine where the current subroutine (vprint)
> # was invoked to $subr. (caller 1)[3] is undefined, use
> # 'main' instead. see perldoc -f caller for details.
>
> $file =~ s!.*/!!;
> # strip everything up to the last '/' character from $file
> # in case (caller)[1] is a full path name.
>
> print STDERR "$subr($file:$line): ", @_;
> # print the subroutine, file, and line number information,
> # along with anything else passed to the sub on STDERR.
> }
>
> HTH,
>
> --jay

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com