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

Discussion in 'Perl Misc' started by dnrg, Jun 26, 2003.

  1. dnrg

    dnrg Guest

    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
    dnrg, Jun 26, 2003
    #1
    1. Advertising

  2. dnrg

    Ted Zlatanov Guest

    Re: Write to a filehandle and to STDOUT without double printstatements?

    On 26 Jun 2003, 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
    Ted Zlatanov, Jun 26, 2003
    #2
    1. Advertising

  3. dnrg <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 26, 2003
    #3
  4. (Tad McClellan) writes:

    > dnrg <> 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\\
    Brian McCauley, Jun 26, 2003
    #4
  5. dnrg

    Matija Papec Guest

    X-Ftn-To: dnrg

    (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
    Matija Papec, Jun 26, 2003
    #5
  6. dnrg

    Matija Papec Guest

    X-Ftn-To: Tad McClellan

    (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
    Matija Papec, Jun 26, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,455
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    2
    Views:
    400
  3. Replies:
    6
    Views:
    120
    Lars Haugseth
    Nov 22, 2006
  4. Adam Funk
    Replies:
    29
    Views:
    229
    Adam Funk
    Jun 30, 2007
  5. Andry

    closing filehandle for tee STDOUT

    Andry, Sep 22, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    96
    Andry
    Sep 23, 2008
Loading...

Share This Page