Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Write to a filehandle and to STDOUT without double print statements?

Reply
Thread Tools

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

 
 
dnrg
Guest
Posts: n/a
 
      06-26-2003
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
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-26-2003
On 26 Jun 2003, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-26-2003
dnrg <(E-Mail Removed)> wrote:

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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-26-2003
(E-Mail Removed) (Tad McClellan) writes:

> dnrg <(E-Mail Removed)> wrote:
>
> > 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?


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\\
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      06-26-2003
X-Ftn-To: dnrg

(E-Mail Removed) (dnrg) wrote:
>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;



--
Matija
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      06-26-2003
X-Ftn-To: Tad McClellan

(E-Mail Removed) (Tad McClellan) wrote:
>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.



--
Matija
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
closing filehandle for tee STDOUT Andry Perl Misc 2 09-23-2008 08:10 AM
Assigning another filehandle to STDOUT, using binmode. Adam Funk Perl Misc 29 06-30-2007 08:22 PM
use print to write binary data to a filehandle seems not correct quakewang@mail.whut.edu.cn Perl Misc 6 11-22-2006 06:30 PM
Filehandle STDOuT reopened as $fh only for input xhoster@gmail.com Perl Misc 2 02-09-2006 04:10 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments