zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    基于消息摆渡节点的DTN路由
    A DTN Congestion Mechanism Based on Distributed Storage
    $(formId).autocomplete is not a function
    [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    ajax提交的中文便会变成乱码
    Ajax原理
    Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.init
    JavaScript 无符号位移运算符 >>> 三个大于号 的使用方法
    普通按钮提交
    jsp的一些基本操作
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350806.html
Copyright © 2011-2022 走看看