Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Problems? Is your data what you think it is?
 
PerlMonks  

Which is a better, more prudent way to redirecting STDOUT?

by snafu (Chaplain)
on Jun 25, 2002 at 15:43 UTC ( [id://177186]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 15: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 16: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 25, 2002 at 22: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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177186]
Approved by ybiC
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.