perl514 has asked for the wisdom of the Perl Monks concerning the following question:
Respected Monks,
I need to redirect the command output to a file in a Perl Script. So far, I have come across the following two ways to do it:
use warnings; use strict; open(PS,"ping localhost|") || die "Failed: $!\n"; while ( <PS> ) { #-- do something here }
Another method is:
use warnings; use strict; open (my $file, '>', 'output.txt') or die "Could not open file: $!"; my $output = `example.exe`; die "$!" if $?; print $file $output;
Please let me know which is the correct way to capture the output. I know there is more than one way to do what I am looking for, but which one of these two are correct in sort of a "Best Practice" way? Thwack me on the head if I am thinking too much and this is trivial :)
Perlpetually Indebted To PerlMonks
http://dwimperl.com/windows.html is a boon for Windows.use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Correct way to capture command output to a file in Perl.
by davido (Cardinal) on May 01, 2013 at 19:31 UTC | |
Re: Correct way to capture command output to a file in Perl.
by Laurent_R (Canon) on May 01, 2013 at 19:58 UTC | |
Re: Correct way to capture command output to a file in Perl.
by kcott (Archbishop) on May 02, 2013 at 00:35 UTC | |
Re: Correct way to capture command output to a file in Perl.
by BillDowns (Novice) on May 01, 2013 at 20:26 UTC | |
by morgon (Priest) on May 02, 2013 at 15:02 UTC | |
Re: Correct way to capture command output to a file in Perl.
by bimleshsharma (Beadle) on May 02, 2013 at 07:38 UTC | |
Re: Correct way to capture command output to a file in Perl.
by perl514 (Pilgrim) on May 02, 2013 at 15:56 UTC |
Back to
Seekers of Perl Wisdom