develooper Front page | perl.beginners | Postings from August 2003

Re: redirecting the output of a command to a file

Thread Previous
From:
John W. Krahn
Date:
August 26, 2003 09:26
Subject:
Re: redirecting the output of a command to a file
Message ID:
3F4B8A2C.87E18876@acm.org
Ivan Novick wrote:
> 
> Hi,

Hello,

> does anyone know syntax to run a command and redirect its output directly to
> a file?
> 
> Equivalent to myCommand > myFile in shell

open my $pipe, 'myCommand |' or die "Cannot open pipe from myCommand:
$!";
open my $fh, '>', 'myFile' or die "Cannot open myFile: $!";
print $fh while <$pipe>;
close $pipe or die "Cannot close pipe from myCommand: $!";


Or if myCommand outputs "binary" data.

open my $pipe, 'myCommand |' or die "Cannot open pipe from myCommand:
$!";
binmode $pipe;
open my $fh, '>', 'myFile' or die "Cannot open myFile: $!";
binmode $fh;
print $fh while read $pipe, $_, 1024;
close $pipe or die "Cannot close pipe from myCommand: $!";



John
-- 
use Perl;
program
fulfillment

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About