Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Why don't file handles have sigils?

by flowdy (Scribe)
on Jul 13, 2014 at 12:09 UTC ( [id://1093427] : note . print w/replies, xml ) Need Help??


in reply to Why don't file handles have sigils?

Why not think of STDIN, STDOUT and STDERR as a kind of constants? And really they look like nothing different, do they? Perl constants are used to be implemented as named subroutines with a zero-argument prototype, returning a filehandle reference.

Unfortunately however, passing an actual user-defined constant filehandle to print() will work only when wrapped in curlies, likewise would any other expression returning a filehandle:

use strict; sub FH () { open my $fh, '>', '/tmp/testfile.txt'; $fh } eval q{ print FH "Hello world!\n"; } or warn "Doesn't work - $@"; print {FH} "This works";

The curlies are recommended best practice for every time you must use type-globs, cf. Conway(2006). Think of it as type-glob derefencing with the star sigil omitted again.

Nowadays you should make lexical filehandles, how this is done has been already outlined in the thread.