zoukankan      html  css  js  c++  java
  • <1> perl概述

    [root@wx03 1]# cat a1.pl 
    $arr=[1,2,3,4,5,6];
    print $arr->[4]."
    ";
    $hash={a=>1,b=>2,c=>3};
    print $hash->{b}."
    ";
    $sub = sub {return "aaaaaaaaa
    "};
    print $sub;
    print "
    ";
    print &$sub;
    print "
    ";
    
    [root@wx03 1]# perl a1.pl 
    5
    2
    CODE(0x251fd20)
    aaaaaaaaa
    
    
    复杂数据结构:
    
    [root@wx03 1]# cat a2.pl 
    $wife{"Jacob"}=["Leah","Rachel","Bilhah","Zilpah"];
    print $wife{"Jacob"}->[2]."
    ";
    [root@wx03 1]# perl a2.pl 
    Bilhah
    
    [root@wx03 1]# cat a3.pl 
    $kids_of_wife{"Jacob"} = {  
    "Leah" => ["Reuben", "Simeon", "Levi", "Judah", "Issachar", "Zebulun"],  
    "Rachel" => ["Joseph", "Benjamin"],  
    "Bilhah" => ["Dan", "Naphtali"],  
    "Zilpah" => ["Gad", "Asher"],};
    print $kids_of_wife{"Jacob"}->{Rachel}->[1]."
    ";  
    [root@wx03 1]# perl a3.pl 
    Benjamin
    
    
    [root@wx03 1]# cat t1.pl 
    $var=5;
    $var .=10;
    print $var."
    ";
    
    [root@wx03 1]# perl t1.pl 
    510
    
    
    
    
    ///
    [root@wx03 1]# cat a5.pl 
    %grades;
    open (GRADES ,test) || die;
    while (my $line=<GRADES>){
    my ($student,$grade) = split (/s+/,$line);
    $grades{$student} .=$grade ." ";
    print $grades{$student}."
    ";
    };
    
    use Data::Dumper;
    $str=Dumper(%grades);
    print "$str is $str
    ";
    [root@wx03 1]# perl a5.pl 
    123 
    123 456 
    $str is $VAR1 = 'abc';
    $VAR2 = '123 456 ';
    
    [root@wx03 1]# cat test 
    abc 123
    abc 456
    
    perl 中的真值:
    
    1.除了""和"0"外,所有字符串都为true;
    
    2.除0以外,所有数字都为true;
    
    3.所有引用都为true;
    
    4. 所有未定义值都为false
    
    
    正则表达式:
    
    [root@wx03 1]# cat a7.pl 
    $var1="a:";
    $var2="aa:";
    $var3=":";
    if ($var1 =~/[a-zA-Z]+:/){print "ok1
    "};
    if ($var2 =~/[a-zA-Z]+:/){print "ok2
    "};
    if ($var3 =~/[a-zA-Z]+:/){print "ok3
    "};
    [root@wx03 1]# perl a7.pl 
    ok1
    ok2
    [root@wx03 1]# 
    
    + 是一个特殊字符,表示"我们前面的内容出现一次或多次"
    
    
    
    
    [root@wx03 1]# cat a8.pl 
    $var1="a";
    $var2="a1";
    if ($var1 =~/a./){print "ok1
    "};
    if ($var2 =~/a./){print "ok2
    "};
    [root@wx03 1]# perl a8.pl 
    ok2
    
    .号表示任意单个字符
    
    [root@wx03 1]# cat a9.pl 
    $var1="a1";
    $var2="1";
    $var3="aaa1";
    if ( $var1 =~ /a*1/ ){print "ok1
    ";};
    if ( $var2 =~ /a*1/ ){print "ok2
    ";};
    if ( $var3 =~ /a*1/ ){print "ok3
    ";};
    [root@wx03 1]# perl a9.pl 
    ok1
    ok2
    ok3
    
    * 表示前面的项出现0次或多次
    
    
    
    perl 贪婪匹配:
    
    
    [root@wx03 1]# cat a10.pl 
    $var="larry:abcdffwrwr:ddad:fsffsf34";
    if ( $var =~/(.+:)/){print "$1 is $1
    ";};
    if ( $var =~/(.*?:)/){print "$1 is $1
    ";};
    [root@wx03 1]# perl a10.pl 
    $1 is larry:abcdffwrwr:ddad:
    $1 is larry:
    
    
    .*? 取消贪婪匹配
    
    
    
    
    
    
    

  • 相关阅读:
    C#自定义控件在添加引用后不显示在工具箱的解决方法
    DevExpress中SearchLookUpEdit用法总结
    div(固定宽度和不固定宽度)居中显示的方法总结
    php面向对象学习笔记
    在php中使用Memcache
    memcache的安装及管理
    使用Quartz.NET cron-like表达式
    SQL CHECK 约束&Case when 的使用方法
    IP地址地区信息查询API接口调用方法
    ASP使用webRequest实现跨域请求
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199993.html
Copyright © 2011-2022 走看看