博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
perl 传递对象到模块
阅读量:5282 次
发布时间:2019-06-14

本文共 2385 字,大约阅读时间需要 7 分钟。

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\n";  print "--------------------\n";print $self->{name};print "\n";print "--------------------\n";bless($self, $class); # 给予对象性质  print "\$self is $self\n";$str=Dumper($self);print "\$str is $str\n";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 "\n";[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\n";print $self->{name};print "\n";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 "----------------------\n";print x2::sum_a($ed,90,66);  ##传入对象到x2模块print "\n";[root@wx03 test]# perl a1.pl ----------------------x2 modulelily255

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6200068.html

你可能感兴趣的文章
info.plist常见属性
查看>>
__setup宏简介
查看>>
转:myeclipse 8.x 插件安装方法终极总结
查看>>
几种情况发生装箱
查看>>
linq to sql语句中转换数据类型和日期操作
查看>>
LeetCode:Climbing Stairs
查看>>
[XAML]命名空间xmlns
查看>>
oracle 不同表空间的数据迁移
查看>>
《The Benefit of Group Sparsity》
查看>>
浅析PHP反序列化漏洞之PHP常见魔术方法(一)
查看>>
【SICP练习】14 练习1.20
查看>>
tomcat性能优化Server——Connector
查看>>
git生成key
查看>>
java之aop
查看>>
Python-类的继承与派生
查看>>
第6次课Windows API绘图函数
查看>>
hdu 4507 吉哥系列故事——恨7不成妻(数位DP,5级)
查看>>
Xcode: 给项目添加framework
查看>>
浅谈字体小图标font awesome,iconfont,svg各自优缺点
查看>>
Java 8 – TemporalAdjusters examples
查看>>