Perl流程中会涉及到很多软件的调用,如果直接把软件的路径写到主程序中,可以执行(my $blastall="/opt/blc/genome/bin/blastall"; );但是碰到复杂的流程时,其中的软件调用很多时,我们一个个写软件的路径也是可以执行的,但是如果软件的路径不存在了,或者软件更新了,那么麻烦就来了,要重头到尾找软件的路径并修改;
现在有一个解决方法,就是将软件的路径写入到一个文件config.txt中,我们只需要在主程序中调用这个文件即可;如何实现呢?就涉及到Perl包的应用;
### software path ####
formatdb=/opt/blc/genome/bin/formatdb
blastall=/opt/blc/genome/bin/blastall
muscle=/opt/blc/genome/bin/muscle
package Soft;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(parse_config);
##parse the software.config file, and check the existence of each software
################################################
sub parse_config{
my ($config,$soft)=@_;
open IN,$config || die;
my %ha;
while(<IN>){
chomp;
next if /^#|^$/;
s/\s+//g;
my @p=split/=/,$_;
$ha{$p[0]}=$p[1];
}
close IN;
if(exists $ha{$soft}){
if(-e $ha{$soft}){
return $ha{$soft};
}else{
die "\nConfig Error: $soft wrong path in $config\n";
}
}else{
die "\nConfig Error: $soft not set in $config\n";
}
}
1;
那么怎么用这个package呢?
可以像正常的module调用那样,use module;
示例如下:
[plain] view plain copy
#! /usr/bin/perl -w
use strict;
use FindBin qw($Bin $Script);
use lib $Bin;
use Soft;
......
......
my $config="$Bin/config.txt";
my $blastall=parse_config($config,"blastall");
......
这样在流程中,看起来似乎多了许多行,但是这个package可以在不同的流程中反复调用,你只需要做好两件事情:
1,把Soft.pm 放到一个适当的位置,如果是调用流程的相对路径,就要用FindBin 模块,如果直接是一个固定的路径,可以这么写:
[plain] view plain copy
#! /usr/bin/perl -w
use strict;
use lib 'home/lxf/';
use Soft;
......
......
my $config="home/lxf/config.txt";
my $blastall=parse_config($config,"blastall");
......
转载自http://blog.csdn.net/hugolee123/article/details/38647011#