• perl 传递对象到模块


    perl 中的对象 就是引用 通过new方法传递数据结构给各个模块
    
    [root@wx03 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); # 给予对象性质  
    print "$self is $self
    ";
    $str=Dumper($self);
    print "$str is $str
    ";
    
    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;
    
    [root@wx03 test]# cat a2.pl 
    unshift(@INC,"/root/test"); 
    require x1;
    $ed=x1->new('lily','29');
    print "
    ";
    
    [root@wx03 test]# perl a2.pl 
    $class is x1
    --------------------
    lily
    --------------------
    $self is x1=HASH(0xd49310)  ###perl里对象就是hash
    $str is $VAR1 = bless( {
                     'name' => 'lily',
                     'age' => '29'
                   }, 'x1' );
    
    			   
    			   
    			 
    			 
    			 
    -----------------------------------------------------
    [root@wx03 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  
                     }; 
    
    
    
    
    
    
    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;
    
    [root@wx03 test]# cat x2.pm 
    package x2;  
    use Data::Dumper; 
    
    sub sum_a { 
      my ($self,              ##传入对象
          $var1,              # Name or IP number of host to ping
          $var2,           # Seconds after which ping times out
          ) = @_;
    print "x2 module
    ";
    print $self->{name};
    print "
    ";
    my $var3= $var1 + $var2 +99;
    return $var3;
     } 
    1;
    [root@wx03 test]# cat a1.pl 
    unshift(@INC,"/root/test"); 
    require x1;
    require x2;
    $ed=x1->new('lily','29');
    print "----------------------
    ";
    print x2::sum_a($ed,90,66);  ##传入对象到x2模块
    print "
    ";
    
    
    [root@wx03 test]# perl a1.pl 
    ----------------------
    x2 module
    lily
    255

  • 相关阅读:
    计算机网络的三种通讯模式(单播,广播,组播)
    java字符串面试题
    java使用纯命令行打包项目
    java字节码的工具(含IDEA插件)
    Spring配置之context:annotation与、component-scan以及annotation-driven
    Java ThreadLocal的使用案例
    对称平方数(to_string函数,stoi函数真香)
    字符串最后一位长度
    缺失的括号
    数三角形
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350806.html
Copyright © 2020-2023  润新知