zoukankan      html  css  js  c++  java
  • perl脚本框架整理

    #开头处常用模块

    #!usr/bin/perl

    use warnings;

    use strict;

    use Getopt::Long;   

    use File::Basename;

    use PerIO::gzip;     #open IN,"<:gzip","$in" or die " $in:$!"; #打开的是一个gzip压缩文件,即$in是file.gz文件

    use Cwd;

    #外部参数设置“模块”—— “use Getopt::Long; ”(另有详解)

    my ($indir,$rep,$dep,$out);
    GetOptions(
      "indir:s" => $indir,
       "rep:s" => $rep,
      "dep:i" => $dep,
      "out:s" => $out,
    );

    #说明“模块-1” 

    =head1 name

      myfile,pl

      #介绍此脚本功能

    =head1 example

      perl  myfile.pl  -参数1  ******** -参数2  ********* -参数  ********   2>myfile.log  

    =head1 description                                            #如果,使用Getop::Long模块,一般此处为参数解释说明

      -参数1      <str>             ********

      -参数2      [str]     ********

      -参数3  [int]    ********

      -参数4  [flt]     ********

      -参数         ********

      -help         help

    =head1 author

       839365149@qq.com

    =head1 version

       1.0  2017-07-15   15:00

    =cut

    die `pod2text $0` if ( $help );                                 # 当用户有输入 -help参数时,进行输出上边的=head1 ...=head1 ......=cut框架中的信息;

    die `pod2text $0` unless ($fq1 && $list1);             # 用于缺少指定的必须参数时,进行报错,输出上边的=head1 ...=head1 ......=cut框架中的信息;

                          # `command`
                          # Perl使用反引号调用外部命令(命令行命令)能够捕获其标准输出,并按行返回且每行结束处附带一个回车。反引号中的变量在编译时会被内插为其值。

                          # pod2text 是命令行函数   #功能输出处理对象(脚本)中的的=head1 ...=head1 ......=cut框架中的信息

    #说明“模块-2” 

    my $usage=<<USAGE;
    Usage : $0
      -indir : directory for input files
       -rep : repeat rate[1.5]
      -dep : depth[0]
      -out : outfile
    USAGE

    #显示时间信息                      

    my $time=`date`;                                                   # date 命令行函数,输出时间

    print STDERR "|-- Start $0 at time: ".$time." ";     # $0指代的就是myfile.pl脚本

    #注释框信息——任意发挥你想要的解释or提示作用

    eg1:解释文件格式

    #####adapter.list format#########################################################################
    # FC81CCCABXX:3:1101:1235:2198#ACTTGAAT/1 49 31 48 iPE-3+ 34 0 17 18 0
    # reads_id reads_len reads_start reads_end adapter_id adapter_len adapter_start adapter_end align_len mismatch
    ############################################################################################

    #内部全局参数设置及初始化

    my($name,$place,$num);

    #内部全局Perl取整、四舍五入、向上取整、向下取整

    取整int 
    四舍五入round 
    向上取整POSIX::ceil 
    向下取整就是int或者POSIX::floor

    其中ceil和floor,要使用库POSIX,在perl源代码里加入

    #!/usr/bin/perl
    use strict;
    use warnings;
    use POSIX;


    #打开外部文件

    if($methy=~/.gz$/){
      open IN,"<:gzip",$methy || die $!;
    }
    else{
      open IN,$methy || die $!;
    }

    #打开输出文件

    if($cout =~ /.gz$/){open OT,">:gzip",$cout;}else{open OT,">$cout";}

    #while循环

    while(<IN>){

      chomp($_); #去掉换行符字符

      next if($_ eq "");#跳过空行

      next if($_=~/^$/)#跳过空行

      last if(not defined $_); #跳过不含字符(包含换行符、空格、空行等)和数值的行,defined判断变量(无论这个变量是否被定义)是否为空(即不包含任何字符和数值)。

    }

    #for循环

    for( $a = 0; $a < 10; $a = $a + 1 ){

      print "a 的值为: $a ";

    }

    #split方便用法

    my $chromosome_2 = (split /./,$chromosome)[0];   

    my ($id, $strand, $chr) = (split / /)[0..2];      #以空格为分割标准为 / / 

    my ($id, $strand, $chr) = (split /s+/)[0..2];      #以空格为分割标准为 /s+/ 

    #偶尔用,但很容易忘记的字符串截取命令substr()

    语法:substr($string,offset,length)
    offset代表起始字符的位置,length代表引用的字符串长度,如果省略length则代表从起始值到字符串的最后一个字符长度。而offset如果是负值的话,就会从字符串右边开始指定字符。

      1. $s=substr("perl5",2,2);#这时$s="rl";  
      2. $s=substr("perl5",2);#这时$s="rl5";  
      3. $s=substr("perl5",-2,2);#这时$s="er"; 

    PS:

    #一些特殊符号的意义

    $0      #指代的就是运行的*.pl 或者  /dir1/dir2/*.pl,即命令行  perl  *.pl    或者 perl   /dir1/dir2/*.pl     

    $?     #如果,报错,$?会产生一个数值,可用下边的perl脚本纠错,查出$?产出的数值是代表什么报错(这个脚本内容没搞明白!)

    if ($? == -1) {
    print "failed to execute: $! ";
    }
    elsif ($? & 127) {
    print "child died with signal %d, %s coredump ",($? & 127), ($? & 128) ? 'with' : 'without';
    }
    else {
    print "child exited with value %d ", $? >> 8;
    }

    $!      #直接文字报错说明原因

    die “$!”    #打印$!值,并结束整个进程 #他自带换行符

     

     

  • 相关阅读:
    对网页图片的增删改管理
    还没搞完的排序(后期更新)
    web实现图片动态
    C++11 笔记
    如何解决刷新系统桌面响应速度很慢的问题
    CGrowableArray解析 _ DXUT容器
    测试...外部指针访问private
    CustomUI Direct3D9_Sample
    缺少.lib文件导致的Link2019 解决方案汇总
    在DirectX9中使用DXUT定制按钮来控制模型旋转的问题
  • 原文地址:https://www.cnblogs.com/Formulate0303/p/7182754.html
Copyright © 2011-2022 走看看