Printing to a file Or STDOUT

H

Hal Vaughan

I thought this was a FAQ, but I can't find it, which means it either isn't
as obvious as I think or I'm just using the wrong search terms.

I want to be able to print to either STDOUT or to a file, so if I have a
series of print statements like:

print $fh "This is a line of text\n";

I'd like to be able to have $fh be able to refer to STDOUT so a command line
option for my program could specify to either save the output to a file or
print it to the console. I know I can just open the file, but how to I set
$fh equal to the actual STDOUT stream?

Thanks!

Hal
 
J

Josef Moellers

Hal said:
I thought this was a FAQ, but I can't find it, which means it either isn't
as obvious as I think or I'm just using the wrong search terms.

I want to be able to print to either STDOUT or to a file, so if I have a
series of print statements like:

print $fh "This is a line of text\n";

I'd like to be able to have $fh be able to refer to STDOUT so a commandline
option for my program could specify to either save the output to a fileor
print it to the console. I know I can just open the file, but how to Iset
$fh equal to the actual STDOUT stream?

my $fh = \*STDOUT;
print $fh "Hello, world\n";
 
X

xhoster

Hal Vaughan said:
I thought this was a FAQ, but I can't find it, which means it either
isn't as obvious as I think or I'm just using the wrong search terms.

I want to be able to print to either STDOUT or to a file, so if I have a
series of print statements like:

print $fh "This is a line of text\n";

I'd like to be able to have $fh be able to refer to STDOUT so a command
line option for my program could specify to either save the output to a
file or print it to the console. I know I can just open the file, but
how to I set $fh equal to the actual STDOUT stream?


$ perl -wle 'use strict; my $fh=*STDOUT; print $fh "foo"'
foo

Xho
 
B

Big and Blue

Hal said:
I want to be able to print to either STDOUT or to a file, so if I have a
series of print statements like:

print $fh "This is a line of text\n";

You could just print to stdout (ie: just print) and do the redirection
to a file on the command line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top