duplicating filehandles

P

pgodfrin

Greetings,
I just cant' remember how to go bout this - I have a piece of code
which does a series of prints to an open file:

open OUTFILE,">myfile" or die;
local $\="\n";
print OUTFILE 'first line of stuff';
print OUTFILE 'second line of stuff';
print OUTFILE 'third line of stuff';

I'd like to send the output to STDOUT instead of OUTFILE, but I don't
want to have to change the print statements en masse to specify
STDOUT.

Is there a way to make the open OUTFILE statement write to STDOUT
instead of the file name specifiied in the open statement? Or is there
another way to make the filehandle OUTFILE point to STDOUT?

pg
 
C

cartercc

Greetings,
I just cant' remember how to go bout this - I have a piece of code
which does a series of prints to an open file:

open OUTFILE,">myfile" or die;
local $\="\n";
print OUTFILE 'first line of stuff';
print OUTFILE 'second line of stuff';
print OUTFILE 'third line of stuff';

I'd like to send the output to STDOUT instead of OUTFILE, but I don't
want to have to change the print statements en masse to specify
STDOUT.

Is there a way to make the open OUTFILE statement write to STDOUT
instead of the file name specifiied in the open statement? Or is there
another way to make the filehandle OUTFILE point to STDOUT?

pg

open OUTFILE,">myfile" or die;
local $\="\n";
print OUTFILE 'first line of stuff';
print STDOUT 'line of stuff to STDOUT'; # this is it!
print OUTFILE 'second line of stuff';
print OUTFILE 'third line of stuff';
close OUTFILE;

CC
 
C

cartercc

Ooooops, didn't read the problem carefully.

Two suggestions:

(1) Use your editor to specify STDOUT on the block of lines you want.

(2) Do this:

open $outfile, ">", "testing.txt";
print $outfile "line 1\n";
{
local $outfile = STDOUT;
print $outfile "line 2\n";
print $outfile "line 3\n";
print $outfile "line 4\n";
}
print $outfile "line 5\n";
close $outfile;

CC
 
P

pgodfrin

That's interesting - I suppose that would work, but I still would have
to do a mass substitution.

Is there a way to say something like

open OUTFILE, ">STDOUT" ;

or maybe

open OUTFILE, ">&1" ;

and then anything written to OUTFILE would go to STDOUT ?

pg
 
R

Ron Bergin

Greetings,
I just cant' remember how to go bout this - I have a piece of code
which does a series of prints to an open file:

open OUTFILE,">myfile" or die;

Better written as:
open my $OUTFILE, '>', 'myfile' or die "can't open 'myfile' $!";
local $\="\n";

select $OUTFILE;
print OUTFILE 'first line of stuff';
print OUTFILE 'second line of stuff';
print OUTFILE 'third line of stuff';

print 'first line of stuff';
print 'second line of stuff';
print 'third line of stuff';
I'd like to send the output to STDOUT instead of OUTFILE, but I don't
want to have to change the print statements en masse to specify
STDOUT.

Is there a way to make the open OUTFILE statement write to STDOUT
instead of the file name specifiied in the open statement? Or is there
another way to make the filehandle OUTFILE point to STDOUT?

# now lets output those same lines to STDOUT

select STDOUT;
print 'first line of stuff';
print 'second line of stuff';
print 'third line of stuff';
 
R

Ron Bergin

That's interesting - I suppose that would work, but I still would have
to do a mass substitution.

Is there a way to say something like

   open OUTFILE, ">STDOUT" ;

or maybe

   open OUTFILE, ">&1" ;

and then anything written to OUTFILE would go to STDOUT ?
If you're wanting to send the output to both filehandles, than you may
want:

use IO::Tee;
http://search.cpan.org/~kenshan/IO-Tee-0.64/Tee.pm
 
G

Gary E. Ansok

That's interesting - I suppose that would work, but I still would have
to do a mass substitution.

Is there a way to say something like

open OUTFILE, ">STDOUT" ;

open OUTFILE, ">&STDOUT";

Look in perldoc perlopentut (specifically the section "Obscure Open
Tricks"), or in perldoc -f open, for more details.

Gary Ansok
 
P

pgodfrin

Yup - that was it... embarrassingly easy...
pg

open OUTFILE, ">&STDOUT";

Look in perldoc perlopentut (specifically the section "Obscure Open
Tricks"), or in perldoc -f open, for more details.

Gary Ansok
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top