Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > closing filehandle for tee STDOUT

Reply
Thread Tools

closing filehandle for tee STDOUT

 
 
Andry
Guest
Posts: n/a
 
      09-22-2008
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

 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      09-22-2008
On Sep 22, 2:16 am, Andry <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Andry
Guest
Posts: n/a
 
      09-23-2008
On Sep 22, 9:46*pm, "C.DeRykus" <(E-Mail Removed)> wrote:
> On Sep 22, 2:16 am, Andry <(E-Mail Removed)> 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
 
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
Assigning another filehandle to STDOUT, using binmode. Adam Funk Perl Misc 29 06-30-2007 08:22 PM
How do I implement the Unix 'tee' function for $stdout? Larry Fast Ruby 13 03-28-2007 09:39 AM
Filehandle STDOuT reopened as $fh only for input xhoster@gmail.com Perl Misc 2 02-09-2006 04:10 PM
SMA Jack-Plug-Jack Tee Jim Wireless Networking 2 10-25-2004 09:45 AM
Write to a filehandle and to STDOUT without double print statements? dnrg Perl Misc 5 06-26-2003 04:39 PM



Advertisments