亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3431 | 回復(fù): 8
打印 上一主題 下一主題

分析一段書本中的perl代碼---簡單 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 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";
復(fù)制代碼

論壇徽章:
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運維版塊每日發(fā)帖之星
日期:2016-04-17 06:23:27操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-24 06:20:0015-16賽季CBA聯(lián)賽之天津
日期:2016-05-06 12:46:59
2 [報告]
發(fā)表于 2013-12-19 14:42 |只看該作者
沒看到 紫色.

如果一個 文件的末尾是 1; 這樣的, 好象相當(dāng)于 exit 1; 或者 return 1

論壇徽章:
0
3 [報告]
發(fā)表于 2013-12-19 14:49 |只看該作者
回復(fù) 2# q1208c

恩,了解了!
但是我執(zhí)行代碼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運維版塊每日發(fā)帖之星
日期:2016-04-17 06:23:27操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-24 06:20:0015-16賽季CBA聯(lián)賽之天津
日期:2016-05-06 12:46:59
4 [報告]
發(fā)表于 2013-12-19 14:52 |只看該作者
回復(fù) 3# hejianbu437


你得把錯誤貼上來, 我才能看看是什么問題, 我水平有限, 暫時還不能只看你貼上來的代碼就預(yù)測可能發(fā)生的錯誤.      

論壇徽章:
0
5 [報告]
發(fā)表于 2013-12-19 14:58 |只看該作者
回復(fù) 4# q1208c

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


   

論壇徽章:
0
6 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2013-12-19 16:36 |只看該作者
{:2_172:}
  1. bless ($ref, $class); # bless anonymous subroutine
  2. }
復(fù)制代碼
|
|
V
  1. };
  2. bless ($ref, $class); # bless anonymous subroutine
復(fù)制代碼
52
  1. }
復(fù)制代碼
|
|
V
  1. }
  2. 1;
復(fù)制代碼
回復(fù) 5# hejianbu437


   

論壇徽章:
0
8 [報告]
發(fā)表于 2013-12-19 17:16 |只看該作者
本帖最后由 wand65 于 2013-12-19 17:18 編輯

回復(fù) 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                              
復(fù)制代碼

論壇徽章:
0
9 [報告]
發(fā)表于 2013-12-20 09:17 |只看該作者
回復(fù) 7# pitonas

搞定 感謝!


   
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP