用一个简单的perl包轻松管理脚本中的软件调用

在用perl写的流程中,有人喜欢直接在脚本定义软件的路径,比如:

my $blastall="/opt/blc/genome/bin/blastall";

这样做,看着好像很省事的样子,但是当一个流程层次关系比较复杂时,而且当其中的路径失效时,麻烦来了:我们需要翻复杂的脚本,重新找到标量的定义,然后修改之。可是,当你把所有调用的软件路径写到配置文件中,并用perl package function调用,当路径失效时,我们只需要在配置文件中修改就可以了。

比如配置文件config.txt是这样的:

###########################
#######software path#######
###########################
prank=/nas/GAG_02/liushiping/GACP/software/evolution/prank-msa/src/prank
GBlocks=/home/lixiangfeng/tools/Gblocks_0.91b/Gblocks
codeml=/share/project002/zhangpei/bin/paml44/bin/codeml
formatdb=/opt/blc/genome/bin/formatdb
blastall=/opt/blc/genome/bin/blastall
muscle=/opt/blc/genome/bin/muscle
###########################
#######END#################
###########################


针对配置文件,我们可以写一个如下的名为Soft.pm的package:

################################################
######This package contains the pathes of many softwares
######You can modify the config.txt when the software path changed
######Original from xiangfeng li,xflee0608@163.com
####################2014-7-5####################
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;              
__END__         


那么怎么用这个package呢?

可以像正常的module调用那样,use module;

示例如下:

#! /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 模块,如果直接是一个固定的路径,可以这么写:

#! /usr/bin/perl -w
use strict;
use lib 'home/lxf/';
use Soft;
......
......
my $config="home/lxf/config.txt";
my $blastall=parse_config($config,"blastall");
......
2.在config文件中按格式要求写好你常用软件的路径,这样管理脚本中涉及的软件路径,是不是很方便呢?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值