• perl 创建包


    <pre name="code" class="python"><pre name="code" class="python">uat-app01:/root/test# cat x1.pm
    package x1;  
    use Data::Dumper;  
    sub new {  
    my $self ={};
    my $invocant = shift;  
    my $class = ref($invocant) || $invocant;  
    my ($name,$age,$starting_position,$monthly_salary)=@_;  
          my $self = {  
             "name" =>$name,  
             "age" =>$age  
                     }; 
    print "$class is $class
    ";  
    print "--------------------
    ";
    print $self->{name};
    print "
    ";
    print "--------------------
    ";
    
    
    
    
    
    
    bless($self, $class); # 给予对象性质  
    return $self;  
    };  
    
    
    sub sum_var { 
      my ($self,
          $var1,              # Name or IP number of host to ping
          $var2,           # Seconds after which ping times out
          ) = @_;
    my $var3= $var1 + $var2;
    return $var3;
     } 
    1;
    
    uat-app01:/root/test# cat x1.pl
    unshift(@INC,"/root/test"); 
    require x1;
    $ed=x1->new('lily','29')
    ;
    print $ed->sum_var(99,5);
    print "
    ";
    print $ed->{name};
    print "
    ";
    
    
    uat-app01:/root/test# perl x1.pl
    $class is x1
    --------------------
    lily
    --------------------
    104
    lily
    当Perl 看到$ed->sum_var(99,5);
    时,它会决定$ed属于哪个类(也就是在其中执行bless的)
    
    
    在这里,它是x1.
    Perl于是就会如下所示调用这个函数
    
    
    x1::sum_var($ed) 换句话说,箭头左边的对象只是作为相应子程序的第一个参数。
    
    uat-app01:/root/test# cat x1.pl
    unshift(@INC,"/root/test"); 
    require x1;
    $ed=x1->new('lily','29');
    print x1::sum_var($ed,90,66);
    print "
    ";
    
    uat-app01:/root/test# perl x1.pl
    $class is x1
    --------------------
    lily
    --------------------
    156
    


    
    
    
    
    
    
    
    
    
                                        
    
  • 相关阅读:
    python删除列表重复元素
    maven常用打包命令
    python开发之函数
    手把手教你用Strace诊断问题
    python引用列表--10
    Python中open函数怎么操作文件--9
    python数据操作--8
    图解源码之FutureTask篇(AQS应用)
    图解源码之java锁的获取和释放(AQS)篇
    图解线程池工作机制,手写线程池?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350928.html
Copyright © 2020-2023  润新知