newbie - Output to multiple Filehandles

Discussion in 'Perl' started by Jeremy Phillips, May 24, 2004.

  1. Hi,

    I am writing one of my first perl scripts and would like to send
    output to a logfile and stdout simultaneously. In addition, I'd like
    to be able to specify that some output only be sent to STDOUT or only
    to the logfile.

    Currently, I am using the following to print to stdout and the logfile
    simultaneously.

    open(STDOUT, "| tee -a $logFileName");

    To print only to the logfile, I create a filehandle for it in the
    usual sense

    open(LOGFILE, ">>$logFileName");

    The problem is that I have no way of specifying output to only
    standard output, leaving the logfile alone. Can anyone tell me how to
    do this? I would greatly appreciate any help.

    Thanks,

    Jeremy
     
    Jeremy Phillips, May 24, 2004
    #1
    1. Advertising

  2. Jeremy Phillips wrote:
    > I am writing one of my first perl scripts and would like to send
    > output to a logfile and stdout simultaneously. In addition, I'd like
    > to be able to specify that some output only be sent to STDOUT or only
    > to the logfile.
    >
    > Currently, I am using the following to print to stdout and the logfile
    > simultaneously.
    >
    > open(STDOUT, "| tee -a $logFileName");


    Just don't do this...

    > To print only to the logfile, I create a filehandle for it in the
    > usual sense
    >
    > open(LOGFILE, ">>$logFileName");
    >
    > The problem is that I have no way of specifying output to only
    > standard output, leaving the logfile alone.


    .... then you won't have this problem.

    > Can anyone tell me how to
    > do this?


    Just print the output twice, once to STDOUT and once to the logfile.
    You can even write a little subroutine (untested, only for clarification):

    sub print_both {
    print STDOUT @_;
    print LOGFILE @_;
    }

    jue
     
    Jürgen Exner, May 25, 2004
    #2
    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. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    648
    Jürgen Exner
    Jul 10, 2004
  2. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    627
    Andrew
    Jul 10, 2004
  3. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    665
    Andrew
    Jul 10, 2004
  4. Sako
    Replies:
    3
    Views:
    5,639
  5. Michael Schmitt

    Reportlab Image object opens filehandles

    Michael Schmitt, Sep 10, 2003, in forum: Python
    Replies:
    4
    Views:
    1,775
    Robin Becker
    Sep 12, 2003
Loading...

Share This Page