Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > duplicating filehandles

Reply
Thread Tools

duplicating filehandles

 
 
pgodfrin
Guest
Posts: n/a
 
      02-02-2009
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

 
Reply With Quote
 
 
 
 
cartercc
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 12:24*pm, pgodfrin <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
cartercc
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 12:31*pm, cartercc <(E-Mail Removed)> wrote:
> > 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?


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
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      02-02-2009
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


On Feb 2, 11:39*am, cartercc <(E-Mail Removed)> wrote:
> On Feb 2, 12:31*pm, cartercc <(E-Mail Removed)> wrote:
>
> > > 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?

>
> 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


 
Reply With Quote
 
Ron Bergin
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 9:24*am, pgodfrin <(E-Mail Removed)> wrote:
> 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';
 
Reply With Quote
 
Ron Bergin
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 9:56*am, pgodfrin <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Gary E. Ansok
Guest
Posts: n/a
 
      02-02-2009
In article <(E-Mail Removed)>,
pgodfrin <(E-Mail Removed)> wrote:
>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
--
Chaos reigns within.
Reflect, repent, and reboot.
Order shall return.
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      02-08-2009
Yup - that was it... embarrassingly easy...
pg

On Feb 2, 12:22*pm, (E-Mail Removed) (Gary E. Ansok) wrote:
> In article <(E-Mail Removed)..com>,
>
> pgodfrin*<(E-Mail Removed)> wrote:
> >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
> --
> Chaos reigns within.
> Reflect, repent, and reboot.
> Order shall return.


 
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
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
FileHandles to string Andrew Perl 1 07-10-2004 06:00 PM
FileHandles to string Andrew Perl 0 07-10-2004 05:18 PM
FileHandles to string Andrew Perl 0 07-10-2004 05:18 PM
newbie - Output to multiple Filehandles Jeremy Phillips Perl 1 05-25-2004 01:38 AM



Advertisments