This strange interest comes from expanding requirements and no time to change design (refactor). This is not good design, sure, but I need to deal with it now and hope to refactor later.
There are a few log files opened early on which are printed to throughout code. The new requirement implies that with a (new) command-line option (--noflag
) one of these log files is irrelevant.
All I could do at the moment is to pad the definition (open my $fh, ...
) and all uses of it (print $fh ...
) with if $flag
. This is clearly bad design and it is error prone (it isn't pretty either).
Is there a way to do something with $fh
when it is associated with the file
so that any following print $fh ...
is accepted by intepreter but will result in simply not running the print
, without error? (Let me imagine something like, say, $fh = VOID if $flag;
.) Or, is there some NULL
stream or such? All I know of are STDOUT
(1), STDERR
(2), and STDIN
(0).
I do not want $fh
to print anywhere else, ideally not even to /dev/null
(if that is possible?). I did look around and couldn't find anything related. I'd appreciate being pointed to information if in fact it is out there already.
Any ideas are appreciated.
PS. First question ever asked here (after years of using SO), please let me know if it's off.
UPDATE
Thanks for responses. They prompt me to add to/refine this question: Are prints marked to go to /dev/null
possibly optimized, so that the 'printing' actually doesn't happen? (While I am still interested in whether it is possible to set a filehandle so to tell to Perl 'do not print here'.)
I am trying to avoid running void (print) statements, without adding conditionals.
Update/Clarification
To summarize a bit from comments (thank you!): This was not a quest for performance optimization. I completely agree with everything said in comments on this. It is simply that executing pointless statements (typically around a million) makes me uneasy. Also, I was curious about some possible dark corner of Perl that I haven't run into. (Most of this has been addressed in answers/comments.)