Front page | perl.beginners |
Postings from August 2009
Re: removing a 'tee'd file handles going forward
Thread Previous
|
Thread Next
From:
Tony Esposito
Date:
August 26, 2009 14:24
Subject:
Re: removing a 'tee'd file handles going forward
Message ID:
140433.36472.qm@web24616.mail.ird.yahoo.com
Thank you all for the input ... I have it working now.
:-)
________________________________
From: Tim Bowden <tim.bowden@mapforge.com.au>
To: Perl Beginners <beginners@perl.org>
Sent: Wednesday, 26 August, 2009 15:20:13
Subject: Re: removing a 'tee'd file handles going forward
On Wed, 2009-08-26 at 18:22 +0000, Tony Esposito wrote:
> Agreed. But the program flow would be such (pseudo-code):
>
> (1) print STDOUT
> print STDERR
> (2) now print to both in one print statement
>
> (3) now go back to
> print STDOUT
> print STDERR
>
> I want to switch back-and-forth between being able to print to STDOUT, STDERR with one 'print' statement and then back ...
>
> HTH
>
>
>
>
>
> ________________________________
> From: Jenda Krynicky <Jenda@Krynicky.cz>
> To: Beginners Perl <beginners@perl.org>
> Sent: Wednesday, 26 August, 2009 10:44:15
> Subject: Re: removing a 'tee'd file handles going forward
>
> Date sent: Wed, 26 Aug 2009 15:36:26 +0000 (GMT)
> From: Tony Esposito <tony1234567893@yahoo.co.uk>
> Subject: removing a 'tee'd file handles going forward
> To: Beginners Perl <beginners@perl.org>
>
> > I want to output to both STDOUT and STDERR at one point in my
> program, then want to separate the two handles going forward in the
> program so that output is sent to STDOUT and STDERR separately. Given
> the code snip below, would the "undef tee" do the trick? > > use
> IO::Tee; > > my $tee = new IO::Tee(\*STDOUT,\*STDERR); >
> $tee->print("Hi\n"); > $tee->flush; > > # some code here ... blah,
> blah, blah ... > # now want to change to set and 'untee' STDOUT and
> STDERR ... > > undef tee; # is this going to do it?
>
> Well it will, but there is no need to do that. You can print to
> STDOUT and STDERR even while the tee exists.
>
> print $tee "Foo\n";
>
> will go to both
>
> print STDOUT "Foo\n";
>
> will go to STDOUT and
>
> print STDERR "Foo\n";
>
> to STDERR.
>
> And
>
> print "Foo\n";
>
> to the select()ed filehandle.
>
> Jenda
>
>
*WARNING: Newbie alert!* ;-)
but the code is tested...
If you set it up right, you can also select the filehandle at runtime if
that helps. If you set autoflush on the fh's, or don't need to use
flush at all then it's even simpler that what I've done here; no need
for references to the fh's, just select the relevant FH directly as
needed.
#!/usr/bin/perl -wT
use strict;
use IO::Tee;
my $tee;
my $somecondition = 1;
my $tee_all = new IO::Tee(\*STDOUT, \*STDERR);
$tee = \*STDOUT;
select $$tee;
print "going to stdout only\n";
$$tee->flush;
# select output fh at runtime
if ($somecondition == 1){
$tee = \$tee_all;
select $$tee;
}
print "this goes to all\n";
$$tee->flush;
$tee = \*STDERR;
select $$tee;
print "this goes to stderr only\n";
$$tee->flush;
Regards,
Tim Bowden
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Thread Previous
|
Thread Next