http://qs321.pair.com?node_id=11007

Buckaroo Buddha has asked for the wisdom of the Perl Monks concerning the following question: (input and output)

I wanted to do something like the following.
open( FILE, $file ); redirect( STDOUT, FILE );
Your mission: define sub redirect. :-)

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by perlmonkey (Hermit) on May 11, 2000 at 07:02 UTC
    The easist way to do this is to use fdopen from IO::Handle:
    use IO::Handle; open INPUT, '<', "input.txt" or die $!; open OUTPUT, '>', "output.txt" or die $!; open ERROR, '>', "error.txt" or die $!; STDIN->fdopen( \*INPUT, 'r' ) or die $!; STDOUT->fdopen( \*OUTPUT, 'w' ) or die $!; STDERR->fdopen( \*ERROR, 'w' ) or die $!; # prints to output.txt: print "Hello Output File\n"; # reads everything from input.txt and prints it to output.txt: print <>; # prints to error.txt: warn "Hello Error File\n";
Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by plaid (Chaplain) on May 10, 2000 at 21:17 UTC
    The typical way to do this would be in the way you run it from the command line, e.g.
    ./script.pl < infile > outfile
    This would take STDIN from infile and put STDOUT to outfile. The sh type shells and csh type shells have different ways to do STDERROR. Under bash, you'd do something like
    ./script.pl < infile > outfile 2> errfile
Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by graff (Chancellor) on Apr 05, 2002 at 04:26 UTC
    This works for me (I haven't tried it on Windoze, but I wouldn't expect that to make a difference):
    open( STDERR, ">", $stderrFileName );
    and likewise for STDOUT. For STDIN, you would replace ">" with "<".

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Buckaroo Buddha (Scribe) on May 10, 2000 at 23:15 UTC
    final self answer ... it works now
    if ($opt_f eq '') { print "Please enter the OUTPUT filename [defaults to scre +en]: "; $output_filename = <STDIN>; chomp($output_filename); if ($output_filename eq '') { } else { # open(SAVOUT, ">&STDOUT"); # save the stdout fhan +dle open(STDOUT,">" . $output_filename); select(STDOUT); # force a flush on stdout so tha +t any output will be in sync. $| = 1; } } elsif ($opt_f ne "DEFAULT") { $output_filename = $opt_f; #open(SAVOUT, ">&STDOUT"); # save the stdout fhandle open(STDOUT,">" . $output_filename); select(STDOUT); # force a flush on stdout so that any ou +tput will be in sync. $| = 1; }
    maybe someone else will get some mileage out of this i know i use perlmonks as a reference all the time <g>

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Buckaroo Buddha (Scribe) on May 10, 2000 at 22:40 UTC
    but i'm trying to do it based on a user determined option
    i'm writing this to be a standalone .EXE that people who don't
    use PERL can run this one script ... thanks to 'perl2exe' this is
    possible
    if someone enters
    myscript.exe -f filename.txt
    i'd like to send it to a file
    but i'd also like to be able to ask them
    print "what output filename do you want?";
    $outfilehandle = <STDIN>;
    ============================================
    i just spoke to a friend of mine and have found
    open(STDOUT, ">>$LOGGINGFILE"); # redirect them to the log file

    i havent tested it yet but it looks about right

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by pwesthagen (Initiate) on Apr 04, 2012 at 22:52 UTC
    Try this, worked for me:
    open FILE, ">$file"; select FILE; # print will use FILE instead of STDOUT print "Hello, world!"; # goes to FILE select STDOUT; # back to normal print "Goodbye."; # goes to STDOUT
Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by scopey (Novice) on Jul 09, 2008 at 22:04 UTC
    I found this thread while looking for a way to redirect Data::Dumper output to a file. Using STDOUT as a filehandle can indeed do this:
    print "Creating .\\ch9.log ..."; open STDOUT, ">ch9.log" or die $!; print Dumper data_for_path ('.'); close STDOUT; print "Done!\n";
    This approach produces an error due to the final print statement (print() on closed filehandle STDOUT at...) Anybody know how I can get STDOUT to direct output back to the screen?
      No need to open STDOUT in the first place - just selecting a file handle makes it the default target for a print operation:
      open my $handle, '>', 'ch9.log' or die $!; select $handle; print Dumper(...); close $handle; select STDOUT; print "Done!\n";
Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Mago (Parson) on Jul 11, 2003 at 15:17 UTC
    #!/usr/bin/perl use strict; use warnings; my $opt_f = './output.txt'; if ($opt_f ne ''){ open(STDOUT, ">> $opt_f"); } print "Start Arquive Mode\n"; print "Hello Perl Monks !\n"; print "End Arquive Mode\n";

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Buckaroo Buddha (Scribe) on May 10, 2000 at 22:55 UTC
    more self answering here
    if ($opt_f eq '') { print "Please enter the OUTPUT filename [defaults to screen]: "; $output_filename = <STDIN>; chomp($output_filename); if ($output_filename eq '') { } else { open(SAVOUT, ">&STDOUT"); # save the stdout fhandle open(STDOUT,">" . $output_filename); select(STDOUT); # force a flush on stdout so that any output +will be in sync. $| = 1; } } elsif ($opt_f ne "DEFAULT") { $output_filename = $opt_f; }
    unfortunately, it dosen't seem to work :(

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Anonymous Monk on Aug 04, 2003 at 21:09 UTC
    Filehandle dup'ing of STDOUT, etc. is covered in "Programming Perl (2nd Ed.)" by Larry Wall, O'Reilly 1996 p. 193 in the section documenting the open() function.

    Originally posted as a Categorized Answer.

Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by Buckaroo Buddha (Scribe) on May 10, 2000 at 20:59 UTC
    hmmm... by the way, the line that says
    redirect(,);
    is supposed to say
    redirect("STDIN","FILE");

    Originally posted as a Categorized Answer.