Perl100

Некоторые интересные аспекты Перла отражены здесь.



[R]
	"Прочти и разберись!"
	Пример предназначен для изучения синтаксиса.
[W]
	"Пиши так!"
	Пример содержит общепринятую идиому языка.
[E]
	"Исполни и убедись!"
	Ответ на вопрос: "Неужели это работает (или не работает)?".
[D]
	"Не обольщайся!" (Perl не повторяется)
	Пример показывает различия между семантически схожими конструкциями.


1. [D] (time => 'flies')  VS  (time, 'flies')


2. [W] my %args = ( align => 'middle', @_ );


3. [RE] $n = pack C4 => $ipaddr =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;


4. [R] [$a => $b] -> [$b <= $a]


5. [DW] open F, $file or die "$!\n"  VS  open F, $file || die "$!\n"
        $foo = $bar || 'DEFAULT'  VS  $foo = $bar or 'DEFAULT'


6. [D] 30 < 299  VS  30 lt 299


7. [DW] *PI = \3.1415  VS  sub PI { 3.1415 }


8. [D] sub s1 { print @_ }
       sub s2 { &s1 }  VS  sub s2 { s1 }


9. [RW] sub inprod(\@\@) {
          local (*a, *b, $n) = @_;
          map { $n += $a[$_]*$b[$_] } 0..$#a;
          $n;
        }
        print inprod @a, @b;


10. [RE] sub p(&$) {
           local $_ = $_[1];
           &{$_[0]};
         }
         p { print } 1;


11. [D] $a = [1, 2, 3]  VS  $a = \(1, 2, 3)


12. [D] sub lv : lvalue { $val }  VS  sub lv : lvalue { return $val }


13. [W] INIT {
          my $count = 1;
          sub next { return $count++ }
        }


14. [RE] sub nextnum {
           my $r = shift;
           return sub { ++$r }
         }


15. [D] $hash{shift} = 1  VS  $hash{+shift} = 1


16. [R] (substr($s,0,1), substr($s,-1)) = (substr($s,-1), substr($s,0,1))


17. [R] sub extrim {
          my @out = @_ ? @_ : $_;
          return wantarray ? @out : "@out";
        }


18. [D] sprintf("%-10s", $text)  VS  pack("A10", $text)


19. [RE] /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?(e([+-]?\d+))?$/i


20. [R] print 'what is it in $_?' if ~$_ & $_;


21. [RE] printf "%02d:%02d:%02d", sub{($_[2], $_[1], $_[0])}->(localtime);


22. [D] print %h,"\n"  VS  print "@{[%h]}\n"


23. [RE] print << x1
         x1


24. [W] $_ = join ' ', split(' ') for @a;


25. [RE] ($_ = <<'  x') =~ s/^\s+//gm;
           ($_ = <<'x') =~ s/^\s+//gm;
           x


26. [RE] sub x{print @_}
         x<<x
          x
         x


27. [D] undef @a  VS  $#a = -1


28. [W] @a = (0) x 01_000;
	@a = (1) x @a;


29. [D] $a = join '', reverse(@a)  VS  $a = reverse(@a)


30. [R] @a[map {$_*2+1, $_*2} 0..$#a/2] = @a;


31. [W] { local $\=$/, print for <*> }


32. [RW] for (@a) { print, last if /foo/ }


33. [RW] while (<>) { print if 1../^$/ }


34. [D] s|\d||g  VS  tr?0-9??d


35. [W] %size = map { $_, -s } <*>


36. [RD] %a = (), grep { !$a{$_}++ } @a  VS  %a = (), @a{@a} = (), keys %a


37. [D] $a = [];
        $a  VS  [ @$a ]


38. [DE] print `$_` while <>  VS  open X,"|$_" while <>


39. [RE] package A; sub B{@_}
         package B; print B A;


40. [D] sub x{{@_}}  VS  sub x{+{@_}}


41. [D] ($a=$b+=1)*=1  VS  $a=($b+=1)*=1


42. [DE] $a[$i++]=$i  VS  $a[$i++]=$i+0


43. [RE] print keys %h if $h{1, 2}=@h{1, 2}=$;=undef


44. [D] $#a = 0  VS  @a = undef


45. [E] local *i=$_, $i++ for split '', $_


46. [E] print scalar( @a = a, $a[$a][$a] )


47. [D] "30" & 3  VS  "30" & "3"


48. [RE] goto '' for /(.)(?{print $1})/g  VS  goto '' while /(.)(?{print $1})/g


49. [WD] { local $/, $s = <F> }  VS  { local $/=$\, $s = <F> }


50. [D] @a=@b if !@a  VS  @a=@a || @b


51. [R] s ggggg


52. [RE] ($a, $a) = a...$a


53. [D] /w/  VS  /w/g


54. [RE] /(?{$w=0,$W=0})(w(?{$w++})|W(?{$W++})|.)*/ && ($w,$W)


55. [D] while (<'$a\\*'>)  VS  while (<$a\\*>)


56. [DR] print $a+ $b  VS  print $a +$b


57. [RE] @g{1..1=~/./g}=1..1


58. [RE] s((.)\1)(\1000)


59. [D] sub a{$a ? $a---&a : 1}  VS  sub a{$a ? --$a-&a : 1}


60. [RE] print+::&\&print


61. [E] $a++; print $a, $a++, $a++


62. [RE] $_="aAa";
         $a=qr/a/;
         print /$a/ig;


63. [D] sub a:lvalue { @a[@_] }
        a(0,1) = (0,1)  VS  (a(0,1)) = (0,1)


64. [E] print if s//v1/e


65. [RE] print if local $_ = undef


66. [W] $/ = \0


67. [E] -f($f).f


68. [D] print -2**4  VS  print (-2)**4


69. [E] (*$,$_)=(sub{q&q&})x2;/./&print &$&$&


70. [D] <$file> VS <${file}>


71. [D] time / 2 ; #/; VS print cos  / 2 ; #/;


72. [E] -+-$_ for qw/1e10 -2 -2e3 0e4/


73. [RE] <{a,b,c,d}{1,2,3}>


74. [RE] {do{last}while 1}  vs  {last while 1}
 
 
75. [E] print sleep 1 while 1;


76. [RE] $a = sub { shift->(print) }; $a->$a;


77. [D] $b ~~ $a; $a == $b VS $a == $b; $b ~~ $a


78. [R] print 0xa+xa>>0b1+xa


79 . [D] @a  vs  @a[0..$#a]