zoukankan      html  css  js  c++  java
  • Perl调用和管理外部文件中的变量(如软件和数据库配置文件)

    编写流程时,有一个好的习惯是将流程需要调用的软件、数据库等信息与脚本进行分离,这样可以统一管理流程的软件和数据库等信息,当它们路径改变或者升级的时候管理起来就很方便,而不需要去脚本中一个个寻找再修改。

    在shell编程中,我们可以通过source config.txt来获取配置文件config.txt中的变量。在Perl中,我们可以将这个功能编写为一个模块,以后就都可以拿来直接用了。

    假设配置文件config.txt如下:

    ###########################
    #######software path#######
    ###########################
    codeml=/share/project002/bin/paml44/bin/codeml
    formatdb=/opt/blc/genome/bin/formatdb
    blastall=/opt/blc/genome/bin/blastall
    muscle=/opt/blc/genome/bin/muscle
    
    ###########################
    #######database path#######
    ###########################
    go=/opt/blc/genome/go.fa
    kegg=/opt/blc/genome/kegg.fa
    

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

    ################################################
    ######This package contains the pathes of softwares and database
    ######You can modify the config.txt when the  path changed
    ################################################
    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 "
    Config Error: $soft wrong path in $config
    ";
                    }
            }else{
                    die "
    Config Error: $soft not set in $config
    ";
            }
    }
    1;              
    __END__         
    

    保存后,在Perl脚本中即可使用该模块:

    #! /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");
    ......
    

    以上是使用相对路径,如果是用绝对路径,如下:

    #! /usr/bin/perl -w
    use strict;
    use lib '/path/soft/';
    use Soft;
    ......
    my $config="/path/soft/config.txt";
    my $blastall=parse_config($config,"blastall");
    ......
    
    

    Ref:https://blog.csdn.net/hugolee123/article/details/38647011

  • 相关阅读:
    关于git---远程
    关于git---主要
    css特效
    Canvas图片压缩
    TypeScript简单介绍
    html 常见兼容性问题及解决方法
    cookies,sessionStorage 和 localStorage 的区别
    vue-element-admin vue.config.js
    ② nodejs + mongodb 搭建服务器
    ① 数据自动填充
  • 原文地址:https://www.cnblogs.com/jessepeng/p/13597684.html
Copyright © 2011-2022 走看看