In the perldoc for print
, it says this should work:
print { $OK ? STDOUT : STDERR } "stuff\n";
But it does not with use strict
, and when I then use quotes like
print { $OK ? "STDOUT" : "STDERR" } "stuff\n";
I get
Can't use string ("STDOUT") as a symbol ref while "strict refs" in use ...
How can I get this structure to work without doing away with use strict
?
Try this:
print { $OK ? *STDOUT : *STDERR } "stuff\n";
The asterisk means the typeglob. Since there is no sigils to denote a file handle, you have to use the typeglob sigil, the asterisk, instead.
To prevent the error message Bareword "STDOUT" not allowed while "strict subs" in use at ...
you'll have to use a typeglob:
#!/usr/bin/perl
use strict ;
use warnings ;
my $OK = 1 ;
printf { $OK ? *STDOUT : *STDERR } "stuff\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