Write to a filehandle and to STDOUT without double print statements?

D

dnrg

I'd like to output the same text to the screen as well as to a file.
I've always used double print statements to achieve this in the past
but it always seemed like a dopey way to do it. Is there a more
elegant, space-saving way to do this? Can you use two or more
filehandles in the same print statement?

Thanks in advance.

- Dana
 
T

Ted Zlatanov

I'd like to output the same text to the screen as well as to a file.
I've always used double print statements to achieve this in the past
but it always seemed like a dopey way to do it. Is there a more
elegant, space-saving way to do this? Can you use two or more
filehandles in the same print statement?

The simple solution is to use a subroutine that does what you want, or
an external program such as tee.

Ted
 
T

Tad McClellan

dnrg said:
I'd like to output the same text to the screen as well as to a file.


Your Question is Asked Frequently.

You are expected to check the Perl FAQ *before* posting to the Perl newgroup.


perldoc -q file

How do I print to more than one file at once?
 
B

Brian McCauley

Your Question is Asked Frequently.

You are expected to check the Perl FAQ *before* posting to the Perl newgroup.


perldoc -q file

How do I print to more than one file at once?

Tad, whilst I approve of RTFFAQ answers in principle, I should point
out that the FAQ fails to mention IO::Tee which is probably the
"right" answer.

Could someone please submit a patch to the FAQ.

I would submit it my self but FAQ maintainers seem to ignore me now
since I once submitted a patch containing a typo.

--- perlfaq5.pod Thu Jun 26 17:14:01 2003
+++ perlfaq5.pod+IO::Tee Thu Jun 26 17:25:05 2003
@@ -658,8 +658,8 @@
for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }

To connect up to one filehandle to several output filehandles, it's
-easiest to use the tee(1) program if you have it, and let it take care
-of the multiplexing:
+easiest to use the IO::Tee module or the tee(1) program if you have
+it, and let it take care of the multiplexing:

open (FH, "| tee file1 file2 file3");

@@ -670,6 +670,11 @@
print "whatever\n" or die "Writing: $!\n";
close(STDOUT) or die "Closing: $!\n";

+ require IO::Tee;
+ my $tee = new IO::Tee \*STDOUT, ">file1", ">file2", ">file3"
+ or die "Teeing off: $!\n";
+ print $tee "whatever\n" or die "Writing: $!\n";
+
Otherwise you'll have to write your own multiplexing print
function--or your own tee program--or use Tom Christiansen's,
at http://www.cpan.org/authors/id/TOMC/scripts/tct.gz , which is

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
M

Matija Papec

X-Ftn-To: dnrg

I'd like to output the same text to the screen as well as to a file.
I've always used double print statements to achieve this in the past
but it always seemed like a dopey way to do it. Is there a more
elegant, space-saving way to do this? Can you use two or more
filehandles in the same print statement?

this is from perl cookbook(slightly modified)

#untested
@fh = (*FH1, *FH2, *FH3);
....
print $_ "writing to all file handles" for @fh;
 
M

Matija Papec

X-Ftn-To: Tad McClellan

Your Question is Asked Frequently.

You are expected to check the Perl FAQ *before* posting to the Perl newgroup.


perldoc -q file

How do I print to more than one file at once?
To connect one filehandle to several output filehandles, you can
use the IO::Tee or Tie::FileHandle::Multiplex modules.

If you only have to do this once, you can print individually to
each filehandle.

for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

According to cookbook this is wrong example, there it claims that strict
can't live with it.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top