从现在开始,每天一点点……

发布新日志

  • perl升级(5.8升级到5.14)

    2011-07-25 21:45:49

    1.下载新的软件
      wget http://www.cpan.org/src/5.0/perl-5.14.1.tar.gz
    2.安装和升级
    mkdir -p /usr/local/perl
    tar zxvf perl-5.14.1.tar.gz
    cd perl-5.14.1
    ./configure -des -D prefix=/usr/local/perl
    make
    make test
    make install
     mv /usr/bin/perl /usr/bin/perl.bak
    ln -s /usr/local/perl/bin/perl /usr/bin/perl
    3.查看perl的版本号
    perl -v
    This is perl 5, version 14, subversion 1 (v5.14.1) built for i686-linux

    Copyright 1987-2011, Larry Wall

    Perl may be copied only under the terms of either the Artistic License or the
    GNU General Public License, which may be found in the Perl 5 source kit.

    Complete documentation for Perl, including FAQ lists, should be found on
    this system using "man perl" or "perldoc perl".  If you have access to the
    Internet, point your browser at http://www.perl.org/, the Perl Home Page.
  • 什么是perl(Practical Extraction and Report Language)

    2011-07-20 12:48:22

    Perl是一种多用途的开源(免费软件)解释型语言。
    它主要用作脚本语言,并且运行在众多平台上。
    依据Wikipedia.org上面的说法:"Perl最初命名为'Pearl',它出自于Gospel of Matthew(《圣经马太福音》)中的'Parable of the Pearl'(珍珠的寓言)。"该寓言的简要内容如下:一位商人正在寻找珍珠,他找到一颗如此贵重、漂亮的珍珠,以至于他情愿倾其所有来购买它。最后,他 甚至比以往更富有。无论你怎样解释这则寓言,它都具有非常积极的寓意。

    如需获取有关Perl 6进展情况的进一步消息,请访问:

    http://www.perl.com/pub/a/2006/01/12/what_is_perl_6.html?page=2


    如需了解Larry Wall生平和Perl的历史,请访问

    http://www.softpanorama.org/People/Wall/index.shtml#Perl_history


  • 目标操作-------perl基础

    2011-07-14 15:29:46

    1.目录句柄
    从目录里获得文件名列表,可使用目录句柄。它不会启用另一个进程。
    下面程序的运行结果包含目录中所有的文件,包括.和..文件,并且没有排序。
    #!/usr/bin/perl
    use strict;
    use warnings;
    use 5.010;

    my $dir_to_process = "/etc";
    opendir DH, $dir_to_process or die "Can not open $dir_to_process:$!";
    foreach my $file (readdir DH){
       print "one file in $dir_to_process is $file\n";
    }
    closedir DH;

    去除所有以点开始的文件:
    my $dir_to_process = "/etc";
    opendir DH, $dir_to_process or die "Can not open $dir_to_process:$!";
    foreach my $file (readdir DH){
       next if $file =~ /^\./;
       #next if $file eq "." or $file eq "..";//去除.和..文件
       #next unless $file =~ /\.conf$/; 找出所有以.conf为结尾的文件
       print "one file in $dir_to_process is $file\n";
    }
    closedir DH;

    找出某个目录下的只读文件,并输出文件的全名,
    my $dirname = "/etc";
    opendir SOMEDIR, $dirname or die "Can not open $dirname:$!";
    while (my $file = readdir SOMEDIR) {
         next if $file =~ /^\./;
         $file = "$dirname/$file";
         next unless -f $file and -r $file;
         print "$file\n";
    }
    closedir SOMEDIR;

    2.perl使用unlink删除文件

    unlink "bb", "cc", "dd";
    unlink glob "*.conf";

    3.重命名文件
    rename "old" "new"

    下面的程序是批量的把以.old结尾的文件命名为以.new结尾的文件foreach my $file(glob "*.old"){
      my $newfile = $file;
      $newfile =~ s/\.old$/.new/;
      if (-e $newfile){
      warn "can't rename $file to $newfile:$newfile exists!\n";

      }elsif(rename $file, $newfile){
      }else{
      warn "rename $file to $newfile failed:$!\n";
      }
    }

    4.链接与文件 link, symlink, unlink
      link函数创建一个硬链接:
      link "bb.new", "bbb" or warn "can not link bb.new to bbb:$!";

      symlink函数创建一个符号链接:
      symlink "cc.new", "ccc" or warn "can not symlink cc.new to
      ccc:$!";
      if (-l "ccc"){
      print "ccc is a symlink.\n";
      }elsif(-e "ccc"){
      print "ccc is a file\n";
      }else{
      print "ccc is not existed.\n";
      }

      unlink可以删除上面的两种连接:
      unlink "ccc", "bbb" or warn "ccc or bbb not exist:$!";

      readlink:返回符号链接指向的位置,或在参数不是符号链接时返回undef。
      my $where =  readlink "ccc";
      print "$where\n";

    5.建立及移除目录mkdir and rmdir
      建立目录调用mkdir函数:
      mkdir "crl", 0755 or warn "can't make crl directory:$!";
      oct函数:强行把字符串当成八进制数处理,无论它是否以零开头。
      my ($name, $perm) = @ARGV;
      mkdir $name, oct($perm) or die "can not create $name: $!";
       ........... 

    6.修改权限chmod(只能接受数字)
      chmod  0755, "fred", "barney";

    7.更改隶属关系
      chown同时改变文件的拥有者和所属的组,但是用户标识和组标识必须以数字的形式来时定,如果不是数字的形式,则要用getpwnam将用户翻译成数字,getgrnam把组名翻译成数字:
      my $user = 101;
      my $group = 104;
      chown $user, $group, "crl";

      将/opt目录下所有的文件所属的用户名和组全部改为 apache和apache:
      defined(my $user = getpwnam "apache") or die "bad user";
      defined(my $group = getgrnam "apache") or die "bad group";
      chown $user, $group, glob "/opt/*";
     
    8.文件句柄

    文件句柄:程序里代表进程与外界之间的I/O联系的名字。
    open的用法:
    open CONFIG, "dino";
    open CONFIG, "<dino";
    open CONFIG, ">dino";
    open CONFIG, ">>dino";

    perl5.6以后,open的另一种新的用法

    open CONFIG, "<", "dino";
    open CONFIG, ">", $file_name;
    open CONFIG,  ">>", &logfile_name();


    作业题目:
    用户键入一个目录名称,并从当前目录切换过去,如果输入的一行空白字符,则会以主目录作为默认目录,所以应切换到他本人的主目录中,切换后输出目录的内容(不含以点开头的文件)并按照英文字母顺序排列,若切换目录失败,则有相应的警告信息,但不必输出目录的内容。
    print "which directory?\n";

    chomp(my $dir = <>);

    if ( $dir =~ /^\s*$/ ){
      chdir or die "Can't chdir to your home directory:$!";
    }else{
      chdir $dir or die "can't change to $dir:$!\n";
    }

    my @files = <*>;
    # my @files = <.* *>; 包含隐藏文件

    foreach(sort @files){
      print "$_\n";
    }
    运行结果:
    /opt/script  
    1
    7
    aa.odd
    bb
    bb.new
    bbb
    cc.new
    ccc
    chai.pl
    chapter10.pl
    chapter13.pl
    chapter14.pl
    chapter15.pl
    crl
    dd.old
    hash
    hw.pl
    learn.pl
    shuzu.pl

    使用文件句柄做上面的题目:

    print "which directory?\n";

    chomp(my $dir = <>);


    if ( $dir =~ /^\s*$/ ){
      chdir or die "Can't chdir to your home directory:$!";
    }else{
      chdir $dir or die "can't change to $dir:$!\n";
    }

    opendir DIRS, "." or die "can't open $dir:$!";

    foreach (sort readdir DIRS){
     #next if $_ =~ /^\./; 不打印以.开头的文件
     print "$_\n";
    }



    编写类似于rm的功能的程序
    ......





  • 智能匹配--given-when结构-------perl基础

    2011-07-12 16:03:12

    1.智能匹配操作符~~
      新的智能匹配操作符会根据需要自己决定用何种方式比较两端的操作数

    下面这两段程序具有相同的效果
    写法一:
    my @name1 = / 1 2 3 4/;
    my @name2 = /1 2 3 4 /;
    my $equal = 0;
    foreach my $index ( 0..$#name1){
    last unless $name1[$index] eq $name2[$index];
    $equal++;
    print "$equal";
    }
    print "the arrays have the same element.\n" if $equal == @name1;

    写法二:

    my @name1 = / 1 2 3 4/;
    my @name2 = /1 2 3 4 /;
    say "the arays have the same element.\n" if @name1 ~~ @name2;

    下面的例子是:在调用某个函数后,检查它的返回结果是否在某个集合中存在的两种写法.
    写法一:
    my @nums = qw( 2 3  42 27);
    my $result = max( @nums );
    my $flag = 0;

    sub max{
      my ($max_so_far) = shift @_;
      foreach( @_ ) {
      if ($_ > $max_so_far ){
        $max_so_far = $_;
       }
     }
      return $max_so_far;
     #print "$max_so_far\n";
    }

    foreach my $num ( @nums ){
       next unless $result == $num;
       $flag = 1;
       #print "$flag\n";
       last;
    }
    print "the value is existed.\n" if $flag;


    写法二:
    my @nums = qw( 2 3  42 27 );
    my $result = max( @nums );
    say "$result\n";
    sub max{
      my ($max_so_far) = shift @_;
      foreach( @_ ) {
      if ($_ > $max_so_far ){
       $max_so_far = $_;
       }
     }
      return $max_so_far;
    }
    print "the values is existed\n" if $result ~~ @nums;

    print "the values is existed\n" if @nums ~~ $result;//为什么这么写没有运行结果,书上说智能匹配对两边操作数的顺序没有要求。这两种写法应该有同样的运行结果才对,可实际结果不对。

    2.智能匹配操作的优先级
    例子                            匹配方式
    %a ~~ %b                     哈希键是否一致
    %a ~~ @b                  至少%a中的一个键在列表@b中
    %a ~~ /fred/              至少一个键匹配给定的模式
    %a ~~ 'fred'              哈希中某一指定键$a{fred}是否存在
     
    @a ~~ @b                  数组是否相同
    @a ~~ /fred/              有一个元素匹配给定的模式
    @a ~~ 123                 至少有一个元素转化位数字后123
    @a ~~ 'fred'              至少有一个元素转化为字符串后是 'fred'

    $name ~~ undef            $name确实尚未定义
    $name ~~ /fred/           模式匹配
    123 ~~ '123.0'            数字和字符串是否大小相等
    'fred' ~~ 'fred'          字符串是否完全相同
    123 ~~ 456                 是否大小相等

    3.given基本语句,given会将参数化名为$_,每个when条件测试都会尝试用智能匹配对$_进行测试。
    use 5.010
    given( $ARGV[0] ){
      when ( /fred/i ) { say 'Name has fred in it' }
      when ( /^fred/ ) { say 'Name start with fred'}
      when ( 'Fred' )  { say 'Name is Fred' }
      default          { say "I don't see a Fred" }
    }

    Continue的用法:

    given( $array[0] ){
      when ( /fred/i ){ say 'name has fred in it';continue }
      when ( /^fred/ ){ say 'name start with fred';continue }
      when ( 'Fred' ) { say 'name is Fred' }
      default         { say "i don't see a Fred" }

    }

    4.多个项目的when匹配
      要遍历多个元素,就不用given,使用foreach的简写方式让他给当前正在遍历的元素起个化名$_,此外,若要使用智能匹配,当前元素就只能是$_.

    #!/usr/bin/perl;
    use 5.010;
    my @array = qw/ Fred is good /;
    foreach (@array) {
      when ( /fred/i ){ say 'name has fred in it';continue }
      when ( /^fred/ ){ say 'name start with fred';continue }
      when ( 'Fred' ) { say 'name is Fred' }
      default         { say "i don't see a Fred" }

    }
    运行结果如下:
    name has fred in it
    name is Fred
    i don't see a Fred
    i don't see a Fred



  • 哈希的简单应用--hash

    2011-07-06 10:19:23

    1.访问哈希
      1)访问哈希元素
        $hash{$some_key}
        $family_name{"fred"} = "rubble"; //哈希键可以是字符串
        $family_name{"fred"} = "astaire";
        $bedrock = $family_name{"fred"};//若对某个已存在的哈希元素赋值,
         就会覆盖之前的值
        $family_name{"wilma"} = "astaire";
        $family_name{"betty"} .= $family_name{"wilma"};//哈希元素会因为赋值而诞生
         访问哈希表里不存在的值就会返回undef;
      2)访问整个哈希
         %family_name; //指代整个哈希
         在列表上下文中,哈希会自动变成一些简单的键值对,叫做哈希松绑。
         @array = %family_name;

    2.哈希赋值
      %new_hash = %old_hash; //用法不常见
      %inverse_hash = reverse %any_hash;  //常见的用法
      my %last_name = (
        "fred" => "flintstone",
        "dino" => "undef",
        "barney" => "rubble",
      );
    3.哈希函数
      keys函数:返回指定的 HASH 里的所有键字组成的一个列表。
      keys %hash;
      values函数:返回一个包含指定散列 HASH 里的所有值的列表。
      values %hash;
     
       #!/usr/bin/perl
       use strict;

       my %hash =(
         "a" => 1,
         "b" => 2,
         "c" => 3,
        );
       my @k = keys %hash;
        print "@k\n";
       my @v = values %hash;
       print "@v\n";
       my $count = keys %hash;
       print "$count\n";
       输出结果:
       c a b
       3 1 2
       3
       each 函数以一次一个键/值对的方式遍历一个散列。在列表环境中,each 返
       回 一个两个元素的列表,该列表包含散列中下一个元素的键字和数值。如果在
       标量环境里调用,each 只是返回散列中下一个元素的键字。如果散列已经全部
       读取完了,那么返回一个空列表,如果你给这个空列表赋值,那么在标量环境中
       会生成一个假值
       each %hash;
        while(($key, $values) = each %ENV){
           print "$key = $values\n";
         }
       delete函数:能从哈希中删除指定的键和对应的值;
       my $person = "betty";
       delete $books{$person};
       exists函数:若要检查哈希中是否有某个键,可用此函数返回真或假值,分别表
       示键存在与否,与对应的值无关。
       print "hey,there is a library card for dino!\n" if exists
       $book{"dino"};
       print "Exists\n" if exists $hash{$key};
      
    4.作业题
      读入用户指定的名字并汇报相应的姓。

  • 子程序--perl

    2011-06-25 09:49:18

    1.当使用use strict编译命令时,perl会要求你一定要用my来声明每个新的变量
      #!/usr/bin/perl
      use strict;

      sub sum_of_fred_and_barney{
      print "hey,you called the sum_of_fred_and_barney subroutine.\n";
      return $fred + $barney;  #从子程序种立刻返回某个值,若没有return会 将子程序最后执行的表达式的直返回;
      print;
      }
      my $fred = 3;
      my $barney = 4;
      my $wilma = &sum_of_fred_and_barney;
      print "wilma is $wilma.\n";
      my $betty = &sum_of_fred_and_barney;
      print "betty is $betty.\n";
    否则会有如下的错误提示:
    Global symbol "$fred" requires explicit package name at ./chapter4.pl line 9.
    Global symbol "$barney" requires explicit package name at ./chapter4.pl line 10.
    Global symbol "$wilma" requires explicit package name at ./chapter4.pl line 11.
    Global symbol "$wilma" requires explicit package name at ./chapter4.pl line 12.
    Global symbol "$betty" requires explicit package name at ./chapter4.pl line 13.
    Global symbol "$betty" requires explicit package name at ./chapter4.pl line 14.

    2.在my不使用()时,只能声明单个词法变量
      my $fred;   正确
      my ($fred, $barney); 正确
      my $fred, $barney;错误

    3.当子程序与perl的内置函数不同名且子程序的声明放在子程序被调用之前时,
      可以将子程序调用符号&以及参数的括号省略掉
     
      #!/usr/bin/perl
      sub division{
      $_[0] / $_[1];
      }
      $quotient = division 10, 2;
      print $quotient;
    4.在子程序中可以使用my操作符来创建私有变量,但每次调用这个字程序的时候,
      这个私有变量就会被重新定义。使用state声明变量,可以在子程序多次调用期
      间保留变量的值并将变量的作用域局限于子程序。(注意:不能初始化state声
      明的数组和散列)
     
      #!/usr/bin/perl
      use strict;
      use 5.010;
      running_sum( 5, 6 );
      running_sum( 1..3 );

      sub running_sum{
        state $sum = 0;
        state @numbers;

        foreach my $number(@_){
          push @numbers, $number;
          $sum += $number;
        }
       say "the sum of (@numbers) is $sum";
      }
       输出结果是:
       the sum of (5 6) is 11
       the sum of (5 6 1 2 3) is 17
    作业题目:求1到1000的和
     
      #!/usr/bin/perl
      use strict;
      use 5.010;
      running_sum(1..1000);
      sub running_sum{
        state $sum = 0;
        state @numbers;
        foreach my $number(@_){
           push @numbers, $number;
           $sum += $numbser;
        }
       say "the sum is $sum";
      }
     

Open Toolbar