Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: filehandle for close

by zbest (Initiate)
on Jan 14, 2013 at 21:04 UTC ( #1013278=note: print w/ replies, xml ) Need Help??


in reply to filehandle for close

Purely by trial and error I figure the following works:

open $files{ "foo" }, " > xyz"; print { $files{ "foo" } } "done\n"; close *{ $files{ "foo" } };
I would love to understand why, though. It is not obvious to me from the documentation.


Comment on Re: filehandle for close
Download Code
Re^2: filehandle for close
by Anonymous Monk on Jan 14, 2013 at 21:48 UTC
Re^2: filehandle for close
by eyepopslikeamosquito (Canon) on Jan 14, 2013 at 21:56 UTC

    The Perl print function, which has a long history and a tricky syntax, takes an optional file handle as its first argument. Without the optional first file handle argument, it prints to the default output handle (typically STDOUT). Moreover, it has a special syntax where its first and second arguments are separated not by a comma (like most Perl functions) but by a space! And because your first argument to print is more complex than a plain scalar variable, you need to specify it in a bare block, for, erm, historical reasons. Note that this special block syntax to print is clearly documented:

    If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead
    However, you then made the (perfectly understandable) "mistake" of assuming that this special print syntax applies to other Perl functions, such as close. When calling most Perl functions, you should not place the first function argument in a block; this is a one-off special case for the print function.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (18)
As of 2014-09-02 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls