in reply to Saving Standard Output (overloading print())

Here is what I use to optionally redirect output:
if ( $opts{log} ) { open STDOUT, ">>".$opts{log} or warn "error opening log: $!"; open STDERR, ">>".$opts{log} or warn "error opening log: $!"; } else { open STDOUT, ">>/dev/null" or warn "error opening /dev/null for wr +iting: $!"; open STDERR, ">>/dev/null" or warn "error opening /dev/null for wr +iting: $!"; }
Make sure to reopen STDOUT before STDERR, or else you won't know if STDOUT is successful.
--
Snazzy tagline here

Replies are listed 'Best First'.
Re: Re: Saving Standard Output (overloading print())
by mischief (Hermit) on Aug 10, 2001 at 13:39 UTC

    Just to note - there's a function in perl called log, so you might want to quote $opts{log} (so it becomes $opts{'log'}).

    update: I'm not saying that it would break anything, just that it might be confusing. When I looked at it, I immediately thought of the log function, not the log key. No biggie, but it might be worth quoting.

      Nyet. If you want log() then you'll need $opts{+log}, just like normal. The default idea is for a hash key to be a string, so it's the log() not the 'log' that needs clarification. Note that $opts{log(5)} isn't ambiguous, only the implicit illegal log of 0 that you get without giving it an argument.

      (I'd love to see the code that uses the log of zero for a hash key...)
      --
      Snazzy tagline here