Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

getting file name/line num info
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

Report this thread to moderator Post Follow-up to this message
Old Post
Manish Sapariya
04-25-05 08:56 PM


Re: getting file name/line num info
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
04-25-05 08:56 PM


Re: getting file name/line num info
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

Report this thread to moderator Post Follow-up to this message
Old Post
Manish Sapariya
04-26-05 01:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:35 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.