I decided to work on improving some code (and start using Perl::Critic) to improve my perl-fu. I have become stuck on what seems like a simple issue.

My program has three log-related options: -l allows the user to specify an output log file and -s instead directs the program output to STDOUT instead of to a file. Throughout the program I print various messages to the log to record the progress. -v specifies a 'verbose' mode where more messages are posted.

I started reading the paltry documentation for the FileHandle module and it has left a lot to be desired (even when going behind it to the IO::File and ::Handle modules). Here is what I have come up with so far, but I feel that there is probably a way to simplify this further.

use FileHandle; our $LOGFH; #can't figure out a way around using a global var here if ( $opt_s ) { $LOGFH = new_from_fd FileHandle fileno(STDOUT), 'a'; } else { $opt_l ||= $DEFAULT_LOG_FILE; $LOGFH = new FileHandle $opt_l, 'a'; } ( ! defined $LOGFH ) and croak 'Could not open logfile, aborting.'; autoflush $LOGFH 1; #autoflush on $LOGFH->print(scalar localtime, ": running MyProgram.\n") or carp 'Could not print to log.';
...during execution optional (verbose) messages are posted (sometimes within subroutines, hence the need for a global filehandle):

$opt_v and $LOGFH->print("$count records were processed.\n") or carp 'Could not print to log.';

...and then, after the program does it's thing:

if ( !$opt_s ) { $LOGFH->print(q{-} x 50, "\n") or carp 'Could not print to log.'; close $LOGFH or croak "Could not close the logfile: $!"; }

I have looked at some of the descriptions for the various logging modules on CPAN, and they all seem like overkill for the very basic logging I want to do. My knowledge of perl always grows when I consult the monks, so I am excited to hear any thoughts on the above code snippets and the problem in general, particularly if there is a logging module that is heavily recommended.


In reply to Best method for opening/using a log by automandc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.