Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1093427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found