Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline selection of FD as in docs does not work with strict refs

Tags:

perl

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?

like image 283
Mazze Avatar asked Feb 27 '17 13:02

Mazze


2 Answers

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.

like image 153
shawnhcorey Avatar answered Nov 14 '22 05:11

shawnhcorey


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" ;
like image 5
dgw Avatar answered Nov 14 '22 03:11

dgw