Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Correct way to capture command output to a file in Perl.

by perl514 (Pilgrim)
on May 01, 2013 at 19:07 UTC ( #1031633=perlquestion: print w/ replies, xml ) Need Help??
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

use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
http://dwimperl.com/windows.html is a boon for Windows.

Comment on Correct way to capture command output to a file in Perl.
Select or Download Code
Re: Correct way to capture command output to a file in Perl.
by davido (Archbishop) on May 01, 2013 at 19:31 UTC

    Have you looked at Capture::Tiny? ...a third way, but it works great.

    From the synopsis:

    use Capture::Tiny ':all'; # capture from external command ($stdout, $stderr, $exit) = capture { system( $cmd, @args ); };

    Dave

Re: Correct way to capture command output to a file in Perl.
by Laurent_R (Parson) on May 01, 2013 at 19:58 UTC

    If you want to capture the output of system command, you can simply use the backticks:

    my @list_of_files = `ls -l`;
Re: Correct way to capture command output to a file in Perl.
by BillDowns (Novice) on May 01, 2013 at 20:26 UTC

    I would probably just use Windows redirection:  `example.exe >output.txt`;
    Then read that file.

      If you do it like that you would not use backticks but system() as you would not capture any output.

      But I would not do it like that as in the error-case it is more difficult to determine what went wrong (e.g. the command you call may be able to run, but may not be able to write to the file).

Re: Correct way to capture command output to a file in Perl.
by kcott (Abbot) on May 02, 2013 at 00:35 UTC

    G'day perl514,

    I'm wondering if the following was closer to what you had in mind:

    perl -Mstrict -Mwarnings -E ' my $cmd = q{ping -c 5 www.perlmonks.org}; my $file = q{./pm_1031633.out}; open my $ping_pipe, "-|", $cmd or die "Pipe from $cmd failed: $!"; open my $out_fh, ">", $file or die "Write to $file failed: $!"; while (<$ping_pipe>) { print { $out_fh } $_; } '
    $ cat ./pm_1031633.out PING perlmonks.org (66.39.54.27): 56 data bytes 64 bytes from 66.39.54.27: icmp_seq=0 ttl=45 time=264.646 ms 64 bytes from 66.39.54.27: icmp_seq=1 ttl=45 time=266.345 ms 64 bytes from 66.39.54.27: icmp_seq=2 ttl=45 time=266.284 ms 64 bytes from 66.39.54.27: icmp_seq=3 ttl=45 time=266.134 ms 64 bytes from 66.39.54.27: icmp_seq=4 ttl=45 time=263.832 ms --- perlmonks.org ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 263.832/265.448/266.345/1.023 ms

    See open for many more examples.

    -- Ken

Re: Correct way to capture command output to a file in Perl.
by bimleshsharma (Beadle) on May 02, 2013 at 07:38 UTC

    See, in your first set of code it will display output on screen instead of writing into file. Second way is good to use.

Re: Correct way to capture command output to a file in Perl.
by perl514 (Pilgrim) on May 02, 2013 at 15:56 UTC

    Venerable Monks,

    Thank you all for taking time to reply. Thank you very much

    Perlpetually Indebted To PerlMonks

    use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
    http://dwimperl.com/windows.html is a boon for Windows.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031633]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-11-27 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (185 votes), past polls