in reply to Re: Dumping data
in thread Dumping data

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?

Replies are listed 'Best First'.
Re: Re: Re: Dumping data
by Roger (Parson) on Dec 31, 2003 at 11:29 UTC
    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";
      Is it just me, or does IO::Capture seem like a stupid idea?
        If you have 100's of print statements in your code, would you rather use a transparent stub or go and modify everyone of your print statements? Please don't make such comment if you don't understand the code.

Re: Re: Re: Dumping data
by Ninthwave (Chaplain) on Dec 31, 2003 at 11:31 UTC

    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