Ah! I don't know why I didn't see that. I could just use a separate MyLoggingModule.pm:
package MyLoggingModule; use strict; use warnings; # ---------- my $logfile; # ---------- sub init { my ($logfile_name) = @_; open($logfile, '>>', $logfile_name) or die "Couldn't open log file."; } sub loggit { my ($log_msg) = @_; print {$logfile} $log_msg, "\n"; } # ---------- 1;
and then my script looks like this:
#!/opt/perl/bin/perl use strict; use warnings; use lib '.'; use MyLoggingModule; use MyModule; use MyOtherModule; # ---------- MyLoggingModule::init('blah.log'); MyLoggingModule::loggit("Doing something in the script..."); MyModule::foo(); MyOtherModule::bar(); MyLoggingModule::loggit("My work here is done.");
and my modules look like this:
package MyModule; use strict; use warnings; use lib '.'; use MyLoggingModule; # ---------- sub foo { # ... # write a log message. MyLoggingModule::loggit("Doing some work in MyModule::foo()"); # ... } 1;
and this:
package MyOtherModule; use strict; use warnings; use lib '.'; use MyLoggingModule; # ---------- sub bar { # ... # write a log message. MyLoggingModule::loggit("Doing some work in MyOtherModule::bar()") +; # ... } 1;
Thanks!
In reply to Re^2: my homemade solution to logging (module writes to same log file as the script). What do you think?
by Anonymous Monk
in thread my homemade solution to logging (module writes to same log file as the script). What do you think?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |