neo's profile十年一场梦 ..BlogListsGuestbookMore Tools Help

Blog


    10/21/2009

    perl 要点 粗略

    1.标量
    ' '     除了\\ \' 可转义外,其他全屏蔽掉 如$x被屏蔽..
    " " 
    字符串连接操作符 .       "a". "b\n"  ---ab
    ....        复制...                  "abc" x 2  --abcabc
    字符串与数字之间自动转换  以连接符为依据转 如 + 则将字符串转为数字再计算
    比较操作符
    ==  eq
    !=  ne
    < lt
    >gt
    <= le
    >=ge
    chomp($text=<>)
     
    2.数组 列表
    defined($a)   判断变量是否赋值,未赋值则返回undef,undef与空串不同
    $#abc   $abc[-1]  数组中最后一个元素
    列表
    (1 2 3..10)     12345678910
    ("a","b","c")
    qw(a b c)
    my($a,$b)=("za" "cv")
    @a  --整个数组
    @a=1..9
    pop@a    将数组a最后一个元素取出返回   返回9  a为1-8
    push(@a,0)   push到数组结尾  a为123456780
    shift @a 开头的出栈
    unshift
    reverse @a    逆序
    标量上下文  列表上下文
    ($x)  =...  列表上下文
    $x    = ...  标量..
    print  列表
    if( ) 标量
    强制指定标量上下文
    scalar(xx)
     
    3.子函数
    sub xxx{
         ....         //子函数 最后一个结果为返回值
    }
    &xxx
    $_[0]   [1] ...  子函数参数
    @_ 参数个数
     
    4.i/o
    <>操作查看@ARGV来决定使用哪些文件,如果为空则使用标准输入流
      打印数组和打印内插一个数组是不同的  如@a=qw(a b c)
    print @a      --abc
    print "@a"   --a b c 有空格  内插时会在每个元素之间自动加上空格
    $r=print (2+3)*4
    此处的print是函数调用,所以只会返回1或0, 如上例  $r=1*4   print输出5
    printf "%s",$a           %s  %d %f   %g
    printf "%12.3f",42+2/3    --oooooo42.667
    print "xxx %2.f%%",5.25/12   --0.44%
    print "xx".("%10s\n" x @a),@a    --可打印任意元素的数组
    文件句柄
    my $out="xxx";
    open LOG,"> $out";
    改变默认的输出
    select LOG   //输出从标准输出改为到句柄LOG
    xxx or die "xxx $!"     //$!为系统错误
    $^I=".bak"   //进行备份保存
     
    5.hash
    打印hash
    use Date::Dumper;
    my %n=(
    "key"=>"value",
    ...
    );
    print Dumper(\%s);
    $hash{$key}
    $hash{"key"}
    keys %hash
    values %hash
    each %hash 返回一对
    exists $book{$key}  //判断hash是否存在这个key
    delete $book{$key}   //将给定key的 hash对删掉
     
    7.正则
    .    匹配除\n之外的任意字符
    *   0-n
    +  1-n
    ?   0,1
    ()
    [abc]   a或b或c
    \d \w \s
    \D
    / /i 不区分大小写
    / /s 使.可以匹配\n
    /^.../
    /...$/
    /...\b/
    $a=~//
    /..(..)..(..)../    $1 $2
    $&   $`   $'       //$`模式匹配到的部分
    {x,y}
    {x,..}
    {x}
    优先级  ()->数量词->锚定
    s/ / /g
    \U   后继全大写
    \L
    \u    后继第一个大写 
    \l
    @a=split /:/,"a:b:c"   @a  (a,b,c)
      $b=join "-",@a    #b  a-b-c
    非贪欲  ?  仅做最小匹配
     
    8控制
    unless   ----if反
    until   ----while反
    print "xxx"  if $n<0;
    if(){
    ..}elsif(){
    ..}
    last= break     //退出循环
    next=continue  //退出本次循环 执行下次
    redo      //跳转到当前循环 开始处
     
    9.文件检测
    if -e $file
    if -M config>28
    if -s $file>100 and -A $file>90
    localtime $time    //将时间戳 1180630098 转为 the may 31 09:00..
     
    10.目录操作
    chdir  "/xx"   改变工作目录  该目录被perl启动后的所有进程继承
    </etc/*>
    目录句柄
    opendir dh,$xx
    for(readdir dh){
    }
    closedir dh
    删除文件  unlink "xx"
    重命名/移动文件
    rename "old"," "
    硬连接
    link "a","b"   b->a
    软连接
    symlink "a","b"
    readlink “b”   找到他的软连接目标a
    创建目录
    mkdir "xx",oct(0755)
    删除目录
    rndir </etc/*>
    修改权限
    chmod 0755,"xx"
    改变所有者
    $user=getpwnam "xx"
    $group=getprnam "xx"
    chown $user,$group,"xx"
     
    11.字符串
    index($s,"w");    找w字符位置 从0开始编号
    substr("fred j. flintstone",8,5)        flint
    substr("fred j. flintstone",0,5)="good"     子串替换
    sprintf "%4d,%.2f%",$a,$b
     
    12.排序
    @n=sort {$a<=>$b}@s
    sub aaa{
    $a<=>$b
    or
    $a cmp $b
    }
     
    13.进程管理
    system "xxx"        创建子进程执行  perl暂停
    exec "xxx"         当前perl进程中执行,perl退出,exec后面的代码没有什么意义
    环境变量path
    $ENV{'PATH'}
    `XX`  类似system中双引号形式
    使用文件句柄来实现命令
    open DATE,"date|";
    my $now=<DATE>;
    print $now;
    信号
    $SIG{"int"} ='xx自定义' ;       程序运行时按ctrl+c 程序会得到该信号,执行自定义函数
     
    14.模块
    安装
    perl Makerfile.pl
    make install
    perl Build.pl
    ./Build install
    使用简单的模块
    use File::basename;
    use File::Spec;
    my $name="/usr/local/bin/perl"
    my $basename=basename $name          得到perl
    my $dirname=File::Basename::dirname $name
    my $new_name=File::Spec->catfile($dirname,$basename);
     
    15.高级perl
    eval捕获
    for(){
    eval{
    ...
    };
    }
    grep {} file
    map {&函数($_)}@data    对整个@执行某个函数变换
    $a=(split /:/)[1]
     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://sh-neo.spaces.live.com/blog/cns!1E3CA285E5F9E122!760.trak
    Weblogs that reference this entry
    • None