被我搜索到了楼主看的书的那一段, 里面的||=sha512($a)中的sha512是个函数,  
这个算法是为了缓存部分的sha512()的值, 以免下次再有相同的输入值的时候,重 
复计算.


($sha512_of{$a} ||= sha512($a)

$sha512_of($a) 如果存在, 取其值(做下面的cmp);
如果不存在, 则取$a来计算sha512($a), 并且把值放到hash变量 $sha512_of{$a} 
中, 以下次可以使用.

因为在例子的算法中, @scripts里存的是单个字符, 所以重复的机会很大, 所以缓 
存起来会减少很大的计算量, 而在加密算法中这个计算又很费时.

�...@script先放了一段拆分成单个字母的英文:

  26 print "\...@script length: ", scalar @script,"\n";
  27
  28
  29 my $i;
  30 my @sorted_scripts;
  31 @sorted_scripts = sort { sha512($a) cmp sha512($b) } @script;
  32 print "sha512() call acount: ",$i,"\n";
  33
  34 $i = 0;
  35 @sorted_scripts = do {
  36     my %sha512_of;
  37     sort { ($sha512_of{$a} ||= sha512($a) )
  38         cmp
  39         ($sha512_of{$b} ||= sha512($b))
  40     } @script;
  41 };
  42 print "sha512() call acount: ",$i,"\n";
  43
  44 sub sha512 {
  45     $i++;
  46     return shift;
  47 }

[0:4...@~] ./t
@script length: 1048
sha512() call acount: 16892
sha512() call acount: 44
[0:4...@~]





附: 楼主应该读的是这本书吧:
http://perl.find-info.ru/perl/028/perlbp-chp-8-sect-1.html




在 2009-9-17,下午11:08, Michael Zeng 写道:

> 这脚本写的不太对吧
>
> 和
> @sorted_scripts
>       =do {
>                             sort { sha512($a) )
>                                          cmp
>                        sha512($b) )
>
>                      }
>                      @scripts;
>             }
> 有什么区别 ?
>
> my %sha512_of; 是新定义的啊, 和没有一样
>
>
>
> 2009/9/17 钟声 <[email protected]>
> @sorted_scripts
>       =do {
>               my %sha512_of;
>               sort { ($sha512_of{$a} ||= sha512($a) )
>                                          cmp
>                        ($sha512_of{$b} ||= sha512($b) )
>
>                      }
>                      @scripts;
>
>             }
> 书上说不要在sort中重新计算排序键。刚看到  ||= 这个符号我半天没反应过来
> 虽然搞懂是什么意思了,但总不像 += 和 --=那么容易接受。大家也都经常这 
> 样用吗?
>
>
> -- 
>            Yours Sincerely
>                    Zeng Hong
>
> >


--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复