Perl线程中的实际多个文件

Perl线程中的实际多个文件,perl,Perl,我需要读取和处理数组中的这么多文件列表。读取的概念写在子程序中,文件作为参数传递。下面的代码一次读取一个文件,程序在读取数组中列出的文件时会花费更多的时间 我想读取不同文件的子例程应执行多次。 #!/usr/bin/perl use strict; opendir DIRS,"/tmp"; my @files=readdir DIRS; closedir DIRS; foreach my $file_read(@files){ &read_files($file_read); }

我需要读取和处理数组中的这么多文件列表。读取的概念写在子程序中,文件作为参数传递。下面的代码一次读取一个文件,程序在读取数组中列出的文件时会花费更多的时间

我想读取不同文件的子例程应执行多次。

#!/usr/bin/perl
use strict;
opendir DIRS,"/tmp";
my @files=readdir DIRS;
closedir DIRS;

foreach my $file_read(@files){
&read_files($file_read);
}

 sub read_files{
  my $R_file=shift;
  open(INP,"<$R_file");
  my $con=<INP>;
  #some manipulation will go here
  close INP;
 }
#/usr/bin/perl
严格使用;
opendir目录,“/tmp”;
my@files=readdir DIRS;
closedir-DIRS;
foreach my$file_read(@files){
&读取文件($file\u read);
}
子读取文件{
my$R_file=shift;

open(INP,“只要它这么简单(我假设您不在子例程中打印到STDOUT):

或者,您可以使用
fork
,它最容易与

你应该看看,也许

有一点需要注意的是:最好使用三个参数open。特别是如果使用插值
open

use threads;

# create the threads
my @threads = map {threads->create(\&read_files,$_)} @files;

# wait until the threads have finished
foreach my $thread (@threads){
  $thread->join();
}