Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

output to STDOUT or a file

by metaperl (Curate)
on Sep 04, 2009 at 15:03 UTC ( #793480=snippet: print w/replies, xml ) Need Help??
Description: 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");
    my $tmp = IO::File->new('tmp.out', 'w');

$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?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://793480]
[Corion]: marto: Oof - that means taking a day off or can you work from home?
[marto]: Corion, I can do some non-technical things, but there's no way for me to connect to the clients network.
[marto]: which is a shame, I had a really productive day yesterday
[Corion]: marto: Meh, so it'll be a day of cleaning out email...
[marto]: and hoped that I'd be able to continue the momentum :)
[marto]: I can't even access client email, nor my employers since those idiots moved to citrix
[marto]: it literally doesn't work. Also, via their citrix interface theres no way to open attachments, or upload an attachment for sending. The company policy is that you email you work account from a personal one, and forward it on from there :/

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-01-16 08:51 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (175 votes). Check out past polls.