Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: blocks and using braces around filehandles with print

by dcd (Scribe)
on Mar 16, 2008 at 22:49 UTC ( [id://674492]=note: print w/replies, xml ) Need Help??


in reply to Re^2: blocks and using braces around filehandles with print
in thread blocks and using braces around filehandles with print

A block is 'code' in this case code that returns a file handle - see how perl deparse's the above code and puts a semicolon before the right curly brace in the code below:
$ perl -MO=Deparse fh.pl
die "Ouch. $!" unless open my $fh, '>', 'foo.txt'; print {$fh;} "hi!\n"; close $fh;
fh.pl syntax OK
(there are probably other methods that you can use to have perl show you what it is "thinking")
perldoc -f print does mention the blocks
Note that if you're storing FILEHANDLEs in an array, or if you're using any other expression more complex than a scalar variable to retrieve it, you will have to use a block returning the filehandle value instead: print { $files[$i] } "stuff\n"; print { $OK ? STDOUT : STDERR } "stuff\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found