closing filehandle for tee STDOUT

Discussion in 'Perl Misc' started by Andry, Sep 22, 2008.

  1. Andry

    Andry Guest

    Hi all,
    I have a problem trying to "tee" STDOUT at some intervals, opening and
    closing filehandles.
    The following is a sample script:
    ******************************************************************
    #!/usr/bin/perl -w

    use File::Tee qw(tee);

    $|=1;

    open my $target1, '>', 'log1.txt';
    tee STDOUT, $target1;
    print "line one\n";
    close $target1;

    open my $target2, '>', 'log2.txt';
    tee STDOUT, $target2;
    print "line two\n";
    close $target2;
    *******************************************************************
    Given the above script, I expect to write "line one" in log1.txt and
    "line two" in log2.txt.
    Instead I get both "line one" and "line two" in log1.txt (while
    log2.txt only includes "line two").
    So I cannot manage to close log1.txt after printing "line one".

    Can anyone help with that?

    Thank you,
    Andrea
     
    Andry, Sep 22, 2008
    #1
    1. Advertising

  2. Andry

    C.DeRykus Guest

    On Sep 22, 2:16 am, Andry <> wrote:
    > Hi all,
    > I have a problem trying to "tee" STDOUT at some intervals, opening and
    > closing filehandles.
    > The following is a sample script:
    > ******************************************************************
    > #!/usr/bin/perl -w
    >
    > use File::Tee qw(tee);
    >
    > $|=1;
    >
    > open my $target1, '>', 'log1.txt';
    > tee STDOUT, $target1;
    > print "line one\n";
    > close $target1;
    >
    > open my $target2, '>', 'log2.txt';
    > tee STDOUT, $target2;
    > print "line two\n";
    > close $target2;
    > *******************************************************************
    > Given the above script, I expect to write "line one" in log1.txt and
    > "line two" in log2.txt.
    > Instead I get both "line one" and "line two" in log1.txt (while
    > log2.txt only includes "line two").
    > So I cannot manage to close log1.txt after printing "line one".
    >



    I believe you'll need to save/reopen STDOUT:

    open my $target1, '>', 'log1.txt' or die $!;
    open(my $oldout, ">&STDOUT")
    or die "Can't dup STDOUT: $!";

    tee STDOUT, $target1;
    print "line one\n";
    close STDOUT;

    open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!";
    open my $target2, '>', 'log2.txt';

    tee STDOUT, $target2;
    print "line two\n";
    close $target2;

    --
    Charles DeRykus
     
    C.DeRykus, Sep 22, 2008
    #2
    1. Advertising

  3. Andry

    Andry Guest

    On Sep 22, 9:46 pm, "C.DeRykus" <> wrote:
    > On Sep 22, 2:16 am, Andry <> wrote:
    >
    >
    >
    > > Hi all,
    > > I have a problem trying to "tee" STDOUT at some intervals, opening and
    > > closing filehandles.
    > > The following is a sample script:
    > > ******************************************************************
    > > #!/usr/bin/perl -w

    >
    > > use File::Tee qw(tee);

    >
    > > $|=1;

    >
    > > open my $target1, '>', 'log1.txt';
    > > tee STDOUT, $target1;
    > > print "line one\n";
    > > close $target1;

    >
    > > open my $target2, '>', 'log2.txt';
    > > tee STDOUT, $target2;
    > > print "line two\n";
    > > close $target2;
    > > *******************************************************************
    > > Given the above script, I expect to write "line one" in log1.txt and
    > > "line two" in log2.txt.
    > > Instead I get both "line one" and "line two" in log1.txt (while
    > > log2.txt only includes "line two").
    > > So I cannot manage to close log1.txt after printing "line one".

    >
    > I believe you'll need to  save/reopen STDOUT:
    >
    >  open my $target1, '>', 'log1.txt' or die $!;
    >  open(my $oldout, ">&STDOUT")
    >       or die "Can't dup STDOUT: $!";
    >
    >  tee STDOUT, $target1;
    >  print "line one\n";
    >  close STDOUT;
    >
    >  open STDOUT, ">&", $oldout   or die "Can't dup \$oldout: $!";
    >  open my $target2, '>', 'log2.txt';
    >
    >  tee STDOUT, $target2;
    >  print "line two\n";
    >  close $target2;
    >
    > --
    > Charles DeRykus


    Thanks a lot Charles!
    That works perfectly.
    Thanks to your suggestion I also found a web page showing several
    examples for STDOUT manipulation:
    http://perldoc.perl.org/functions/open.html

    Andrea
     
    Andry, Sep 23, 2008
    #3
    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. Joe Blow
    Replies:
    0
    Views:
    417
    Joe Blow
    Nov 14, 2007
  2. Bil Kleb

    Blixy Tee Bus Stop

    Bil Kleb, Mar 27, 2006, in forum: Ruby
    Replies:
    1
    Views:
    92
    Geoff
    Mar 27, 2006
  3. Larry Fast
    Replies:
    13
    Views:
    299
    Erik Veenstra
    Mar 28, 2007
  4. dnrg
    Replies:
    5
    Views:
    134
    Matija Papec
    Jun 26, 2003
  5. Replies:
    6
    Views:
    797
    Ted Zlatanov
    Jun 13, 2006
Loading...

Share This Page