• 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

  • 相关阅读:
    springboot解析带控制字符\"的json字符串
    SpringBoot使用JSR356 Websocket使用configurator = SpringConfigurator.class自动注入Spring IOC的bean报错: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
    REST API 调用新方法
    mac终端提示zsh: operation not permitted:怎么办?
    (6.1)分栏布局
    (6.2)弹性布局
    比较器 Comparison 与 IComparer
    requireJS 的回顾
    分享刚出炉的基于Blazor技术的Web应用开发框架
    架构必备技能第一谈
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200069.html
Copyright © 2020-2023  润新知