#开头处常用模块

#!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."\n";     # $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源代码里加入

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. 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\n";

}

#split方便用法

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

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

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: $!\n";
}
elsif ($? & 127) {
print "child died with signal %d, %s coredump\n",($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
print "child exited with value %d\n", $? >> 8;
}

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

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

  1.  

perl常用总结的更多相关文章

  1. Perl常用语法

    本文主要为了方便阅读.编写perl脚本而记录的常用的命令.语法. 一.条件判断语法 注意,数字 0, 字符串 '0' . "" , 空 list () , 和 undef 为 fa ...

  2. perl常用代码

    字符串联结和重复操作符   联接: .  重复:x  联接且赋值(类似+=): .=例:  $newstring = "potato" . "head";  $ ...

  3. Perl常用特殊变量

    perl 内置变量 $- 当前页可打印的行数,属于Perl格式系统的一部分 $! 根据上下文内容返回错误号或者错误串 $” 列表分隔符 $# 打印数字时默认的数字输出格式 $$ Perl解释器的进程I ...

  4. perl 常用命令

    过滤重复字符 perl -e '$_=<STDIN>; @in = split; if (@in < 100){ @out = grep {! $hash{$_}++ } @in;  ...

  5. Perl 常用的小细节总结

    1.命令行:perl -c perl.pl  #用来检验Perl脚本有没有错误: 2.vi perl.pl打开脚本,ESC+:set nu 回车,给每行加上行号:

  6. perl常用字符串函数

    1.$position = index(string,substring,skipchars): 该函数返回子串substring在字符串string中的位置,如果不存在,则返回-1:参数skipch ...

  7. perl oneline

    可参考博客:http://blog.csdn.net/carzyer/article/details/5117429 Perl常用命令行参数概览 -e 指定字符串以作为脚本(多个字符串迭加)执行 -M ...

  8. 拾遗:Perl 基础语法

    Perl 常用的命令行参数 -i:将处理结果直接写入文件,可以通过 -i.bak 或 -i"/tmp/orig_*" 等形式,在修改之前进行备份 -e:启用 perl 的命令行模式 ...

  9. Perl入门(一)Perl的基本类型及运算符

    在学习Perl的基础之前,还是希望大家有空去看以下Perl的简介.百度百科 一.Perl的基本类型 Per的基本类型分为两种:数值型和字符串型. 数值型可细分为 整数型.如123. 浮点型.如123. ...

随机推荐

  1. PAT1111 Online Map【最短路】【dfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805358663417856 题意: 给定一个图,每天边上有时间和路 ...

  2. Gym 101873K - You Are Fired - [贪心水题]

    题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...

  3. python扫描端口脚本

    # -*- coding:utf8 -*- # # Python: 2.7.8 # Platform: Windows # Authro: wucl # Program: 端口扫描 # History ...

  4. bounds的应用

    frame是参考父view的坐标系来设置自己左上角的位置.设置bounds可以修改自己坐标系的原点位置,进而影响到其“子view”的显示位置.   向上滚动scrollview,我们就不断增加scro ...

  5. git常用操作命令使用说明

    设置用户名和邮箱 git config --global user.email 'xxx' git config --global user.name 'xxx' 创建分支 git branch xx ...

  6. vs安装问题

    1 首先windows update异常,导致vs2015的一个安装不上,先试着修一下: https://support.microsoft.com/zh-cn/help/2629484 如果提示:“ ...

  7. [js] 渲染树构建、布局及绘制

    渲染树构建.布局及绘制

  8. Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnection

    Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnec ...

  9. AIX更改用户组

    1:先 修改 user: smit chuser 把id 号给改了.成功! 2:在 修改group 的时候报警告: smit chgroup : can not update the /etc/pas ...

  10. [openjudge-贪心]装箱问题

    题目描述 描述 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6.这些产品通常使用一个 6*6 ...