Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390981
  • 博文数量: 117
  • 博客积分: 4416
  • 博客等级: 上校
  • 技术积分: 1135
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-08 14:16
个人简介

一生醉生忘死,就让他继续下去吧!

文章分类

全部博文(117)

分类: LINUX

2012-11-12 09:46:33

object:scalars,arrays,hashes
operate:grep,map,sort,print
 
 
@results=grep EXPR,@input_list;=========EXPR可以是 $_>10,也可以是/4$/
 
  1. my @odd_digit_sum=grep digit_sum_is_odd($_),@input_numbers;
  2. sub digit_sum_is_odd {
  3.              my $input=shift;
  4.              my igits=split //,$inupt;
  5.              my $sum;
  6.              $sum =$_ for @digits;
  7.              return $sum % 2;
  8.      }
 
eval  1 ====>trap eroors
example:



         
  1. eval {$average=$total/$count};
  2. print "error:$@" if $@;

eval  2 ======>calculate EXPR;

example:

  1. eval '$sum = 2 2';
  2. print "The sum is $sum\n";

         

  1. #!/usr/bin/perl
  2. my @outarray=qw(teacher mother father grandpa doctor sisiter telephone);
  3. sub truth {
  4.         print "@_\n";
  5.         @input=shift;
  6.         @inarray=qw(lunar final mother telephone tonado);
  7.         for $i (@inarray) {
  8.                 if (grep $i eq $_,@_) {     ####### @_presents all arguments
  9.                                 print "$i is the same word\n";
  10.                 }

  11.         }
  12. }
  13. &truth('gil',@outarray);

array reference/dereference

  1. reference:
  2. $array=\@array1
  3. print @array1;
  4. print $array1[1];

  5. dereference:
  6. print @{$array};
  7. print ${$array}[1];

Example quote reference:

  1. #!/usr/bin/perl
  2. my @outarray=qw(teacher mother father grandpa doctor sisiter telephone);
  3. my $file=\@outarray;
  4. sub truth {
  5.     @inarray=qw(lunar final mother telephone tonado);
  6.     for $i (@inarray) {
  7.         if (grep $i eq $_,@{$file}) {
  8.                 print "$i is the same word\n";
  9.         }

  10.     }
  11. }
  12. &truth('gil',@outarray);


######Print the reref reref of the array

  1. #!/usr/bin/perl
  2. sub initial {
  3. my @outarray=qw(teacher mother father grandpa doctor sisiter telephone);
  4. my @skip_outarray=('skip',\@outarray);
  5. my @array=(\@outarray,
  6.          \@skip_outarray,
  7.          );
  8. print "\@array==========@array\n";
  9. print "\@array[0]=======@array[0]\n";
  10. print "\@array[1]=======@array[1]\n";
  11. print "\@\{\$array[0]\}=======@{$array[0]}\n";
  12. print "\@\{\$array[1]\}=======@{$array[1]}\n";
  13. print "\@{\${\$array[1]}[1]}========@{${$array[1]}[1]}\n";

  14.          }
  15. initial();

 ########Simplfy

  1. #!/usr/bin/perl
  2. sub initial {
  3. my @outarray=qw(teacher mother father grandpa doctor sisiter telephone);
  4. my @skip_outarray=('skip',\@outarray);
  5. return (\@outarray,
  6.          \@skip_outarray,
  7.          );
  8. }
  9. my @all=initial();
  10. print "\@all====@all\n";
  11. print "\@all==========@all\n";
  12. print "\@all[0]=======@all[0]\n";
  13. print "\@all[1]=======@all[1]\n";
  14. print "\@\{\$all[0]\}=======@{$all[0]}\n";
  15. print "\@\{\$all[1]\}=======@{$all[1]}\n";
  16. print "\@{\${\$all[1]}[1]}========@{${$all[1]}[1]}\n";

####create temporary variable
  1. #!/usr/bin/perl
  2. my $data1;
  3. {
  4.         my @temp=qw( one two three);
  5.         $data1=\@temp;
  6. }
  7. print "\@temp====@temp\n"; ####@temp went away
  8. print "\$data1======$data1\n";
  9. print "\@\$data1======@$data1\n";
##########above mentioned example is same with the next:
  1. #!/usr/bin/perl
  2. my $array=[qw( one two three)];
  3. print "\@\$array=======@$array\n";
#####grep expr
  1. #!/usr/bin/perl
  2. my @input_numbers=qw(1 2 4 7 8 16 32 64);   ########qw 中不能加逗号
  3. my @indices_of_odd_digit_sums=grep {
  4.         my $number=$input_numbers[$_];
  5.         my $sum;
  6.         for (split //,$number) {           ##########数字拆分为数组
  7.         $sum =$_;                          ###########数字累加
  8. }
  9.         $sum%2;                            ############余数为1
  10. } 0..$#input_numbers;
  11. print "@indices_of_odd_digit_sums\n";
  12. my @odd_digit_sums=@input_numbers[@indices_of_odd_digit_sums];############指定顺序号的数字
  13. print "@odd_digit_sums\n";
#########use module retrieve
  1. #!/usr/bin/perl
  2. use Storable;
  3. use Data::Dumper;
  4. my @data1=qw(one won);
  5. my @data2=qw(two too to);
  6. push @data2,\@data1;
  7. push @data1,\@data2;
  8. store [\@data2,\@data1], 'some_file';
  9. my $result=retrieve 'some_file';
  10. print Dumper($result);
#########use package
#!/usr/bin/perl
{package Animal;
        sub speak{
                my $class=shift;
                print "a $class goes",$class->sound,"!\n";
        }
}
{package Cow;
        @ISA=qw(Animal);           =========>把cow看作Animal的一个子包
        sub sound {"moooooo"};
}
{package dog;
        @ISA=qw(Animal);           =========>why @ISA can't print
        sub sound {"wangwang"};
}
Cow->speak;
dog->speak;

############Accessing the Instance Data

#!/usr/bin/perl
{package Horse;
        @ISA=qw(Animal);
        sub sound {'neigh'};
        sub name {
                my $self=shift;
                $$self;                    =====>self rereference
        }
}
my $she='Mr.King';
my $who=\$she;
my $tv_horse=bless $who,'Horse';
print $tv_horse->name,"\n";


####################debug value
#!/usr/bin/perl
{package Animal;
    sub speak {
        my $class=shift;
        print "$class goes",$class->sound,"\n"
    }
    sub name {
            my $self=shift;
            $$self;
    }
    sub named {
        my $class=shift;
        my $name=shift;
        bless \$name,$class;
    }
}
{package Horse;
        @ISA=qw(Animal);
        sub sound {'neigh'}
}
my $tv_horse=Horse->named('Mr.Ed');
$tv_horse->speak;                    =============>a Horse=SCALAR(0x...) goes neigh





#########Add parameters to a method


#!/usr/bin/perl
{package Animal;
    sub named {
            my $class=shift;
            my $name=shift;
            bless \$name,$class;
#            print "\$class=====$class====\n";
    }
    sub name {
            my $either=shift;
            ref $either
                    ?    $$either #it's an instance,return name
                    :    "an unamed $either";#it's a class
            }
    sub speak {
            my $either=shift;
            print $either->name,'goes',$either->sound,"\n";
            }
    sub eat {
            my $either=shift;
            my $food=shift;
            print $either->name,"eats $food.\n";
    }
}
{package Horse;
        @ISA=qw(Animal);
        sub sound{'neigh'}
}
{package sheep;
        @ISA=qw(Animal);
        sub sound{'miemie'};
    }
my $tv_horse=Horse->named('Mr.Ed');
$tv_horse->eat('hay');
#Animal::eat($tv_horse,'hay');     ==============>same with the last line
sheep->eat('grass');




#########bless 人变狗
########<>
#!/usr/bin/perl
 use strict;
 use person;
 use dog;
 sub main()
 {
         my $object = {"name" => "tom"};
         # 先把"tom"变为人
         bless($object, "person");
         $object->sleep();
         $object->study();
         # 再把"tom"变为狗
         bless($object, "dog");
         $object->sleep();
         $object->bark();
         # 最后,再把"tom"变回人
         bless($object, "person");
         $object->sleep();
         $object->study();
 }
&main;

##########<>
 #!/usr/bin/perl -w
 package person;
 use strict;
 sub sleep() {
         my ($self) = @_;
         my $name = $self->{"name"};
         print("$name is person, he is sleeping\n");
 }
 sub study() {
         my ($self) = @_;
         my $name = $self->{"name"};
         print("$name is person, he is studying\n");
 }
 return 1;
#############<>
 #!/usr/bin/perl -w
package dog;
 use strict;
 sub sleep() {
         my ($self) = @_;
         my $name = $self->{"name"};
         print("$name is dog, he is sleeping\n");
 }
 sub bark() {
         my ($self) = @_;
         my $name = $self->{"name"};
         print("$name is dog, he is barking\n");
 }
 return 1;

###############<>

#!/usr/bin/perl
{package Animal;
    sub speak {
        my $class=shift;
        print $class->name," goes ",$class->sound,"\n";
    }
    sub name{
                my $self=shift;
                ref $self
                ? $$self
                : $self;
        }
    sub named {
                my $class=shift;
                my $name=shift;
                bless \$name,$class;
    }
    sub DESTROY{
            my $self=shift;
            print '[',$self->name,"has died.]\n";
    }
    sub eat {
            my $class=shift;
            my $food=shift;
            print $class->name," eat","  $food\n";
    }
}
{package Cow;
        @ISA=qw(Animal);
        sub sound{'neigh'};
       
}

sub feed_a_cow_named {
        my $name=shift;
        my $cow=Cow->named($name);
        $cow->eat('grass');
        print "Returning from the subroutine.\n";
}
print "Start of program.\n";
my $outer_cow=Cow->named('Bessie');
print "Now have a cow named",$outer_cow->name,"\n";
feed_a_cow_named('Gwen');
print "Returned from subroutine.\n";




###############Destroy Barn1
#!/usr/bin/perl
{package Animal;
    sub speak {
        my $class=shift;
        print $class->name," goes ",$class->sound,"\n";
    }
    sub name{
                my $self=shift;
                ref $self
                ? $$self
                : $self;
        }
    sub named {
                my $class=shift;
                my $name=shift;
                bless \$name,$class;
    }
    sub DESTROY{
            my $self=shift;
            print '[',$self->name," has died.]\n";
    }
    sub eat {
            my $class=shift;
            my $food=shift;
            print $class->name," eat","  $food\n";
    }
}
{package Cow;
        @ISA=qw(Animal);
        sub sound{'neigh'};
       
}

sub feed_a_cow_named {
        my $name=shift;
        my $cow=Cow->named($name);
        $cow->eat('grass');
        print "Returning from the subroutine.\n";
}
{package Barn;
        sub new {bless [],shift};
        sub add {push @{ shift},shift};
        sub contents {@{ shift}};
        sub DESTROY {
                my $self=shift;
                print "$self->nameaa is being destroyed\n";
                for ($self->contents){
                        print ' ',$_->name," goes homeless.\n";
                }
        }
}
my $barn=Barn->new;
$barn->add(Cow->named('Bessie'));
$barn->add(Cow->named('Gwen'));
print "Burn the barn:\n";
$barn=undef;
print "End of program.\n";

#############Destroy Barn 2#########################3

阅读(1907) | 评论(0) | 转发(0) |
0

上一篇:Perl教程

下一篇:数据整理

给主人留下些什么吧!~~