Perl ush生成一个数组,所以将“print”行替换为:while(my$result=){push@results,$result;},而且您可以处理@result数组 for $str (@files) { my($command
ush生成一个数组,所以将“print”行替换为:while(my$result=){push@results,$result;},而且您可以处理@result数组Perl ush生成一个数组,所以将“print”行替换为:while(my$result=){push@results,$result;},而且您可以处理@result数组 for $str (@files) { my($command,perl,Perl,ush生成一个数组,所以将“print”行替换为:while(my$result=){push@results,$result;},而且您可以处理@result数组 for $str (@files) { my($command) = "perl command.pl ".$str; exec( $command ); } $file=$ARGV[0].".csv"; #code that counts rows here print $rowcount;
for $str (@files)
{
my($command) = "perl command.pl ".$str;
exec( $command );
}
$file=$ARGV[0].".csv";
#code that counts rows here
print $rowcount;
for $str (@files)
{
my($command) = "perl command.pl ".$str;
print `$command`;
}
package MyCommand;
use Exporter;
our @EXPORT = qw( command );
sub command {
my $file = $_[0] . '.csv';
...
return $rowcount;
}
1;
use MyCommand;
...
my @rowcounts;
for my $str (@files) {
push @rowcounts, command($str);
}
#!/usr/bin/perl
use threads;
use Thread::Queue;
my @workers;
my $num_threads = 10;
my $queue = new Thread::Queue;
my $total_ines = 0;
for (0..$num_threads-1) {
$workers[$_] = new threads(\&worker);
}
while ($_ = shift @ARGV) {
$queue->enqueue($_);
}
sub worker() {
while ($file = $queue->dequeue) {
#line counting code here
global_counter($lines_counted);
}
}
sub global_counter() :locked {
#add to the number of lines counted
$total_lines += shift
}
for (0..$num_threads-1) { $queue->enqueue(undef); }
for (0..$num_threads-1) { $workers[$_]->join; }
print $total_lines;
# -- fork.pl -------------------------
for (1..3) {
open my $PIPE, "perl command.pl |";
print "catch: $_\n" while(<$PIPE>);
close $PIPE;
}
# -- command.pl ----------------------
print rand(1);
catch: 0.58929443359375
catch: 0.1290283203125
catch: 0.907012939453125
#!/usr/bin/perl -w
use strict;
my $files = qw/one.csv two.csv three.csv/;
my $command = "perl command.pl";
my @pipes;
foreach (@files) {
my $fd;
open $fd, "-|", "$command $_" and push @pipes, $fd;
};
my $sum = 0;
foreach my $pp (@pipes) {
$sum += $_ if defined ($_=<$pp>);
};
print $sum;