Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Perl Monks, i'm somewhat a of Perl Rookie and i'm looking to write a program where I need to maintain a log file. The program will be farily long, and i'd like to simply be able to call a subroutine with a message and have it timestamp it and output it to the log file. For example:
sub log_it { my $fh = shift; my $message = shift; my $timestamp = time; print $fh $timestamp . ": " . $message . "\n"; }
I'd like to be able to call log_it from anywhere without having to pass $fh (the filehandle for the logfile) though every subroutine in the entire program just so I can log to that file. I know one solution would be open and close the log file every single time, but that dosn't seem that practical to me (or a very good practice). I realize there a numerous logging modules in CPAN, most of which are overkill for what I want to do, but i'd rather learn the best (object oriented?) method for doing this without using a global filehandle.
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "Global" filehandle without using global variables
by tobyink (Canon) on Jun 18, 2012 at 21:22 UTC | |
by Anonymous Monk on Jun 18, 2012 at 21:44 UTC | |
|
Re: "Global" filehandle without using global variables
by BrowserUk (Patriarch) on Jun 18, 2012 at 21:03 UTC | |
|
Re: "Global" filehandle without using global variables
by fishmonger (Chaplain) on Jun 18, 2012 at 22:06 UTC | |
|
Re: "Global" filehandle without using global variables
by ikegami (Patriarch) on Jun 18, 2012 at 21:15 UTC |