duplicating filehandles

Discussion in 'Perl Misc' started by pgodfrin, Feb 2, 2009.

  1. pgodfrin

    pgodfrin Guest

    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
    pgodfrin, Feb 2, 2009
    #1
    1. Advertising

  2. pgodfrin

    cartercc Guest

    On Feb 2, 12:24 pm, pgodfrin <> 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
    cartercc, Feb 2, 2009
    #2
    1. Advertising

  3. pgodfrin

    cartercc Guest

    On Feb 2, 12:31 pm, cartercc <> 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
    cartercc, Feb 2, 2009
    #3
  4. pgodfrin

    pgodfrin Guest

    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 <> wrote:
    > On Feb 2, 12:31 pm, cartercc <> 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
    pgodfrin, Feb 2, 2009
    #4
  5. pgodfrin

    Ron Bergin Guest

    On Feb 2, 9:24 am, pgodfrin <> 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';
    Ron Bergin, Feb 2, 2009
    #5
  6. pgodfrin

    Ron Bergin Guest

    On Feb 2, 9:56 am, pgodfrin <> 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
    Ron Bergin, Feb 2, 2009
    #6
  7. In article <>,
    pgodfrin <> 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.
    Gary E. Ansok, Feb 2, 2009
    #7
  8. pgodfrin

    pgodfrin Guest

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

    On Feb 2, 12:22 pm, (Gary E. Ansok) wrote:
    > In article <..com>,
    >
    > pgodfrin <> 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.
    pgodfrin, Feb 8, 2009
    #8
    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. Jeremy Phillips

    newbie - Output to multiple Filehandles

    Jeremy Phillips, May 24, 2004, in forum: Perl
    Replies:
    1
    Views:
    2,126
    Jürgen Exner
    May 25, 2004
  2. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    630
    Jürgen Exner
    Jul 10, 2004
  3. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    601
    Andrew
    Jul 10, 2004
  4. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    648
    Andrew
    Jul 10, 2004
  5. Sako
    Replies:
    3
    Views:
    5,597
Loading...

Share This Page