Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Quick question, I have a loop .. something like:
while(<INA> ) { .... ... } else {.... } } print "$A,$B,n/a\n" if $i==0
My question is I currently get my results dumping out to screen from the print statement above, How can I pipe these results to a "results" file? Thanks.

Replies are listed 'Best First'.
Re: Dumping data
by Roger (Parson) on Dec 31, 2003 at 10:24 UTC
    There are many ways you can do this. The simplest method, without modifying your script, is to redirect STDOUT of the script to the results file when you run the script from the command-line:
    perl script.pl > results.txt
    A slight variant if you are working on a Unix system is to use the tee program:
    perl script.pl | tee results.txt
    The tee program allows you to redirect the STDOUT of script.pl to the results.txt file, while at the same time display the output to screen (or pipe to some other program down the chain).

      Humm i was hoping for a way to do this automatically? so when i run the perl file it prints the data on screen and creates the results.txt file?
        Just add the following stub to the beginning of your perl program will allow your script to print to both STDOUT and results.txt. The stub works transparently so you do not have to change your print statements.
        use strict; use warnings; # ---- add this stub to the start of your program ---- use IO::File; use IO::Capture::Stdout; our $capture; BEGIN{ $capture = IO::Capture::Stdout->new(); $capture->start(); } END{ $capture->stop(); my $f = new IO::File "results.txt", "w" or die "Can not create file" +; while (defined ($_ = $capture->read())) { print $f "$_"; print "$_"; } } # ---- your stuff here ---- print "Test Line One\n"; print "Test Line Two\n"; print ""; print "Test Line Three\n";

        You can open a file for output and print to that outfile.

        open (MYOUTFILE, ">outfile.txt") or die "Can't create outfile: $!"; # Other code print MYOUTFILE "$A,$B,n/a\n" if $i==0; print STDOUT "$A,$B,n/a\n" if $i==0; # Other code close MYOUTFILE;

        or

        if ($i==0){ print MYOUTFILE "$A,$B,n/a\n"; print STDOUT "$A,$B,n/a\n"; }

        There are variations on this and you can include screen output, append to files, and more. Your best bet is to read up on filehandles and open and print.

        Resources

        Update: Closed off some tags that messed up the code and picked up some dropped end tags.

        "No matter where you go, there you are." BB
Re: Dumping data
by ViceRaid (Chaplain) on Dec 31, 2003 at 10:24 UTC

    Your shell should allow you to do something like this. When you run the perl programme, try something like this:

    perl my_perl.pl > results.txt

    Then, you shouldn't see any output to the screen, but the results should be in results.txt. Is this the sort of thing you wanted?

    HTH
    ViceRaid

Re: Dumping data
by chimni (Pilgrim) on Dec 31, 2003 at 11:33 UTC

    Well Just under your print statement you can open a file and dump data to a filehandle.
    my $results="/home/aa/results"; open(MY_HANDLE, "> $results"); print MY_HANDLE $A,$B; close MY_HANDLE;

    This way you will have the results in a file.
Re: Dumping data
by davido (Cardinal) on Jan 01, 2004 at 06:16 UTC
    I haven't seen this mentioned yet, but when I saw it in your original question I did a double-take.

    while () { ... } else { ... }
    ... is not a valid Perl syntax. There's no 'else' on while loops.


    Dave

Redirecting STDOUT
by oha (Friar) on Dec 31, 2003 at 12:53 UTC
    open(STDOUT, '>', "dump.out") || die "cannot redirect stdout";
    redirects STDOUT to dump.out

    see perldoc -f open