snafu has asked for the wisdom of the Perl Monks concerning the following question:

Both of the following examples of code work, however, being new to this kind of thing in Perl I am curious what those among the monks who know more than I do think.

The following is what I am currently using. The latter is what I changed .

if ( exists $ARGS{'q'} ) { $CRON_MODE = 1; sysopen(LOG,"$LOGDIR$LOGFILE", O_CREAT | O_APPEND | O_WRONLY) or die("Unable to open logfile: $LOGDIR$LOGFILE\n"); *STDOUT = \*LOG; print "Now running in cron/quiet mode to $LOGDIR$LOGFILE.\n"; }
And what I changed from:
if ( exists $ARGS{'q'} ) { $CRON_MODE = 1; close(STDOUT); sysopen(STDOUT,"$LOGDIR$LOGFILE", O_CREAT | O_APPEND | O_WRONL +Y) or die("Unable to open logfile: $LOGDIR$LOGFILE\n"); print "Now running in cron/quiet mode to $LOGDIR$LOGFILE.\n"; }
Which of these ways is more preferred, if there is such a thing in this subject?

TIA

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...

Replies are listed 'Best First'.
Re: Which is a better, more prudent way to redirecting STDOUT?
by belg4mit (Prior) on Jun 25, 2002 at 19:50 UTC
    Personally I would open and not sysopen, and quite possibly not even bother to close in the first place.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Which is a better, more prudent way to redirecting STDOUT?
by Jenda (Abbot) on Jun 25, 2002 at 20:19 UTC

    IMHO, as a rule of thumb, if you can do something without typeglobs (the *WHATEVER thingies), do not use them. You should only play with them if you have to.

      Jenda

Re: Which is a better, more prudent way to redirecting STDOUT?
by Zaxo (Archbishop) on Jun 26, 2002 at 02:15 UTC

    A couple more possibilities:

    { local *STDOUT; open STDOUT, '>>', "$logbin/$logname" or die $!; # ... }
    or else
    open LOG, '>>', "$logbin/$logname" or die $!; # ,,, { my $oldout = select LOG; # ... log stuff select $oldout; }
    The choice depends most on what fits your program design.

    The case you need to worry about with your code is when STDOUT has been redirected someplace else. You want to restore it to what it was when you exit the logging block. Hemce local.

    After Compline,
    Zaxo