I'm curious if I can toggle between printing to STDOUT or STDERR based on some value or inline expression (without using an if statement).
print ($someFlag ? STDOUT : STDERR) "hello world!"
Obviously, that syntax doesn't work.
I think this will do what you want:
print {$someFlag ? *STDOUT : *STDERR} "hello world!";
A similar example can be seen in the documentation for print. Use typeglobs so that it will run under use strict
.
Another strategy is to define your own printing function that will behave differently, depending on the value of $someFlag
.
Do you need to evaluate for each call to print
?
If not, would this work for you:
my $redir = $someFlag ? STDOUT : STDERR;
print $redir "hello world!\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With