home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl / 5.10.0 / List / Util.pm
Encoding:
Perl POD Document  |  2009-06-26  |  1.9 KB  |  105 lines

  1. # List::Util.pm
  2. #
  3. # Copyright (c) 1997-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6.  
  7. package List::Util;
  8.  
  9. use strict;
  10. use vars qw(@ISA @EXPORT_OK $VERSION $XS_VERSION $TESTING_PERL_ONLY);
  11. require Exporter;
  12.  
  13. @ISA        = qw(Exporter);
  14. @EXPORT_OK  = qw(first min max minstr maxstr reduce sum shuffle);
  15. $VERSION    = "1.19";
  16. $XS_VERSION = $VERSION;
  17. $VERSION    = eval $VERSION;
  18.  
  19. eval {
  20.   # PERL_DL_NONLAZY must be false, or any errors in loading will just
  21.   # cause the perl code to be tested
  22.   local $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
  23.   eval {
  24.     require XSLoader;
  25.     XSLoader::load('List::Util', $XS_VERSION);
  26.     1;
  27.   } or do {
  28.     require DynaLoader;
  29.     local @ISA = qw(DynaLoader);
  30.     bootstrap List::Util $XS_VERSION;
  31.   };
  32. } unless $TESTING_PERL_ONLY;
  33.  
  34. # This code is only compiled if the XS did not load
  35. # of for perl < 5.6.0
  36.  
  37. if (!defined &reduce) {
  38. eval <<'ESQ' 
  39.  
  40. sub reduce (&@) {
  41.   my $code = shift;
  42.   no strict 'refs';
  43.  
  44.   return shift unless @_ > 1;
  45.  
  46.   use vars qw($a $b);
  47.  
  48.   my $caller = caller;
  49.   local(*{$caller."::a"}) = \my $a;
  50.   local(*{$caller."::b"}) = \my $b;
  51.  
  52.   $a = shift;
  53.   foreach (@_) {
  54.     $b = $_;
  55.     $a = &{$code}();
  56.   }
  57.  
  58.   $a;
  59. }
  60.  
  61. sub first (&@) {
  62.   my $code = shift;
  63.  
  64.   foreach (@_) {
  65.     return $_ if &{$code}();
  66.   }
  67.  
  68.   undef;
  69. }
  70.  
  71. ESQ
  72. }
  73.  
  74. # This code is only compiled if the XS did not load
  75. eval <<'ESQ' if !defined ∑
  76.  
  77. use vars qw($a $b);
  78.  
  79. sub sum (@) { reduce { $a + $b } @_ }
  80.  
  81. sub min (@) { reduce { $a < $b ? $a : $b } @_ }
  82.  
  83. sub max (@) { reduce { $a > $b ? $a : $b } @_ }
  84.  
  85. sub minstr (@) { reduce { $a lt $b ? $a : $b } @_ }
  86.  
  87. sub maxstr (@) { reduce { $a gt $b ? $a : $b } @_ }
  88.  
  89. sub shuffle (@) {
  90.   my @a=\(@_);
  91.   my $n;
  92.   my $i=@_;
  93.   map {
  94.     $n = rand($i--);
  95.     (${$a[$n]}, $a[$n] = $a[$i])[0];
  96.   } @_;
  97. }
  98.  
  99. ESQ
  100.  
  101. 1;
  102.  
  103. __END__
  104.  
  105.