Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

output to STDOUT or a file

by metaperl (Curate)
on Sep 04, 2009 at 15:03 UTC ( [id://793480]=CUFP: print w/replies, xml ) Need Help??

apparently FileHandle is deprecated in favor of IO. Here is some code that allows you to write to either STDOUT or a file.
use IO qw(File Handle); my $io = do { if (shift) { my $tmp = IO::Handle->new; die "open failed: $!" unless $tmp->fdopen(fileno(STDOUT),"w"); $tmp; } else { my $tmp = IO::File->new('tmp.out', 'w'); $tmp; } }; $io->print("Some text\n");

Replies are listed 'Best First'.
Re: output to STDOUT or a file
by bv (Friar) on Sep 04, 2009 at 18:12 UTC

    I'm confused about this line:

    if (shift)

    What are you shifting off? Or is it just a general "put a condition here" line? Also, how does this benefit over:

    my $fh; if(my $filename = shift) { open $fh, '>', $filename or die "Can't open $filename: $!"; } else { open $fh, '>&', STDOUT; } print $fh "Where do you want to go today?\n";
    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found