免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3411 | 回复: 8
打印 上一主题 下一主题

分析一段书本中的perl代码---简单 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-12-19 14:06 |只看该作者 |倒序浏览

请帮忙分析一下,紫色部分的1;是什么作用,另外代码二为什么运行会报如下错误

Can't call method "set" on unblessed reference at /tmp/hjb17.pl line 9.

  1.   1
  2.   2   package Student;
  3.   3   sub new { # Constructor
  4.   4   my $class = shift;
  5.   5   my $data={};
  6.   6   our $students;
  7.   7   my $ref = sub { # Closure
  8.   8   my ($access_type, $key, $value) = @_;
  9.   9   if ($access_type eq "set"){
  10. 10   $data->{$key} = $value; # $data still available here
  11. 11   }
  12. 12   elsif ($access_type eq "get"){
  13. 13   return $data->{$key};
  14. 14   }
  15. 15   elsif ($access_type eq "keys"){
  16. 16   return (keys %{$data});
  17. 17   }
  18. 18   elsif ($access_type eq "destroy"){
  19. 19   $students--;
  20. 20   return $students;
  21. 21   }
  22. 22   else{
  23. 23   die "Access type should be set or get";
  24. 24   }
  25. 25   print "New student created, we have ", ++$students,
  26. 26   " students.\n";
  27. 27   bless ($ref, $class); # bless anonymous subroutine
  28. 28   }
  29. 29   } # End constructor
  30. 30   sub set{
  31. 31   my($self,$key,$value) = @_; # $self references anonymous sub
  32. 32   $self->("set",$key,$value);
  33. 33   }
  34. 34   sub get{
  35. 35   my ($self,$key) = @_;
  36. 36   return $self->("get", $key);
  37. 37   }
  38. 38   sub display{
  39. 39   my $self = shift;
  40. 40   my @keys = $self->("keys");
  41. 41   @keys=reverse(@keys);
  42. 42   foreach my $key (@keys){
  43. 43   my $value = $self->("get",$key);
  44. 44   printf "%-25s%-5s:%-20s\n",$self, $key,$value ;
  45. 45   }
  46. 46   print "\n";
  47. 47   }
  48. 48   sub DESTROY{
  49. 49   my $self = shift;
  50. 50   print "Object going out of scope:\n";
  51. 51   print "Students remain: ", $self->("destroy"), "\n";
  52. 52   }
  53. 53   [color=Magenta]1;[/color]

  54.   [code]
  55.   1 #!/usr/bin/perl
  56.   2 use Student;
  57.   3 $ptr1 = Student->new(); # Create new students
  58.   4 $ptr2 = Student->new();
  59.   5 $ptr3 = Student->new();
  60.   6 print "$ptr1\n";
  61.   7 print "$ptr2\n";
  62.   8 print "$ptr3\n";
  63.   9 $ptr1->set("Name", "Jody Rogers"); # Set data for object
  64. 10 $ptr1->set("Major", "Law");
  65. 11 $ptr2->set("Name", "Christian Dobbins");
  66. 12 $ptr2->set("Major", "Drama");
  67. 13 $ptr3->set("Name", "Tina Savage");
  68. 14 $ptr3->set("Major", "Art");
  69. 15 $ptr1->display(); # Get all data for object
  70. 16 $ptr2->display();
  71. 17 $ptr3->display();
  72. 18 print "\nThe major for ", $ptr1->get("Name")," is ", $ptr1->get("Major"), ".\n\n";
复制代码

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 2013-12-19 14:42 |只看该作者
没看到 紫色.

如果一个 文件的末尾是 1; 这样的, 好象相当于 exit 1; 或者 return 1

论坛徽章:
0
3 [报告]
发表于 2013-12-19 14:49 |只看该作者
回复 2# q1208c

恩,了解了!
但是我执行代码2的时候为什么会报错呢?


   

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
4 [报告]
发表于 2013-12-19 14:52 |只看该作者
回复 3# hejianbu437


你得把错误贴上来, 我才能看看是什么问题, 我水平有限, 暂时还不能只看你贴上来的代码就预测可能发生的错误.      

论坛徽章:
0
5 [报告]
发表于 2013-12-19 14:58 |只看该作者
回复 4# q1208c

我贴了。在第二行啊,哥。。。


   

论坛徽章:
0
6 [报告]
发表于 2013-12-19 16:34 |只看该作者
本帖最后由 wand65 于 2013-12-19 16:40 编辑

。。。。。。。。。。。。。。。。。。。。

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
7 [报告]
发表于 2013-12-19 16:36 |只看该作者
{:2_172:}
  1. bless ($ref, $class); # bless anonymous subroutine
  2. }
复制代码
|
|
V
  1. };
  2. bless ($ref, $class); # bless anonymous subroutine
复制代码
52
  1. }
复制代码
|
|
V
  1. }
  2. 1;
复制代码
回复 5# hejianbu437


   

论坛徽章:
0
8 [报告]
发表于 2013-12-19 17:16 |只看该作者
本帖最后由 wand65 于 2013-12-19 17:18 编辑

回复 1# hejianbu437

7 楼是对的,
(第二条 的那个 1;楼主是有的,没问题)
}” 这个部分 是 教程的错误,我说的是perl 实例精解中文版。


多谢七楼指点
  1. else{                                                   
  2. die "Access type should be set or get";                 
  3. }                                                      
  4. print "New student created, we have ", ++$students,     
  5. " students.\n";
  6. };                                    
  7. bless ($ref, $class); # bless anonymous subroutine      
  8.                                                    
  9. } # End constructor                              
复制代码

论坛徽章:
0
9 [报告]
发表于 2013-12-20 09:17 |只看该作者
回复 7# pitonas

搞定 感谢!


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP