Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn 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
Post Follow-up to this messageThanks,
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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.