in reply to Reference sub name
The caller function will let you do that.
Example:
sub foo { my $calling_sub = (caller 1)[3]; $calling_sub = 'the main block' if !defined $calling_sub; print "called from: $calling_sub\n"; } sub bar { foo(); } bar(); # prints: called from: main::bar foo(); # prints: called from: the main block
Personally, when I need additional log information depending on context, I prefer to use explicit parameters for the logging function. This way, I have more control on the logging protocol, and the individual context tags at each logger call make it easy to map log messages back to the related pieces of code without exposing program internals in the logs. I do not depend on a one-to-one relationship between subroutine names and things worth while to log, either.
Example:
# using Sys::Syslog log levels sub open_db { my $dbname = shift; # ... if (defined $dbh) { log_this(LOG_NOTICE, 'OPEN_DB', $dbname, 'db connection establ +ished'); } else { log_this(LOG_ERR, 'OPEN_DB_NOK', $dbname, 'db connection faile +d'); } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Reference sub name
by BrowserUk (Patriarch) on Jul 27, 2008 at 10:41 UTC | |
by martin (Friar) on Jul 27, 2008 at 13:56 UTC |
In Section
Seekers of Perl Wisdom