The following rough code example will get you what you want. The parameter
passed to "caller" tells the function how many frames to go back in the call
stack. The fourth list element (which is number 3, starting at 0) contains
the name of the subroutine at that frame.
####################################################
Code
####################################################
sub bbb {
my @call_info = caller(1);
print "Subroutine is: ", $call_info[3], "\n";
}
sub aaa {
bbb;
}
aaa;
####################################################
Output
####################################################
Subroutine is: main::aaa
-----Original Message-----
From: Rajeev Rumale [mailto:[EMAIL PROTECTED]]
Sent: Saturday, July 07, 2001 01:22
To: Me; [EMAIL PROTECTED]
Subject: Re: getting the calling function name.
Thanks "me".
But the what i need is the grep the name of the sub which is calling the
loging sub.
As you have suggested "caller" only gives only the name of current sub
routine that is &debugLogger it self.
And I had to do call the "caller" before calling the "debugLogger" and pass
all the info to it.
What i need is the the name of the sub rotuine which is "calling" the sub
"&debuLogger".
Consider the following subs
----------------
sub someSub{
statement 1;
statement 2;
statement 3;
&debugLogger($message);
&anotherSub;
}
sub anotherSub{
statement 1;
statement 2;
statement 3;
&debugLogger($message);
}
sub debugLogger {
my $message = "@_";
$sub_name = ""; # Get the subroutine name.This is what i need to grep.
open (LOGGER, ">> my.log") || die " unable to ope the log file";
print LOGGER "\n----------------------------------------";
print LOGGER "\nCalled from $sub_name.";
print LOGGER "\n$message";
print LOGGER "\n----------------------------------------";
close LOGGER;
}
So if the sub "someSub" is called
The information I expect in the file is .
------------------------------------------------
called from someSub
Here is the message.
------------------------------------------------
------------------------------------------------
called from anotherSub
Here is the message.
------------------------------------------------
with regards
Rajeev Rumale
----------------------------------------------------------------------------
-----
Artificial Intelligence is no match for Natural Stupidity.
----------------------------------------------------------------------------
-----
----- Original Message -----
From: "Me" <[EMAIL PROTECTED]>
To: "Rajeev Rumale" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Monday, August 06, 2001 3:49 PM
Subject: Re: getting the calling function name.
> perldoc -f caller
> perldoc Carp.pm
> http://search.cpan.org/search?mode=module&query=assert
>
> > Hello Every Body,
> >
> > I need some help.
> > I am using a sub routine to log all the messages to be used for
> debugging
> > purpose.
> > ie. &debugLogger($debug_message);
> > Since very similare kind of messages are genrated at serveral places,
> I add
> > the function name.
> > i.e., &debugLogger("inside xxxxx \n $debug_message");
> >
> > I feel it would be better as a prgramming style if sub debugLog is
> able to
> > find the name of the "calling function" or the name of the function
> > "calling" this sub and genrate the appropriate message.
> >
> >
> > with regards
> >
> >
> > Rajeev Rumale
> >
> >
> > ***********************************************************
> > "The human race has one really effective weapon, and that is
> laughter."
> > ***********************************************************
> >
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]