Good Evils In Perl

4,869
-1

Published on

Published in: Technology, Business
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,869
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
88
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Good Evils In Perl

  1. Good Evils in Perl Kang-min Liu <gugod@gugod.org>
  2. $speaker.meta • • http://handlino.com/ Kang-min Liu gugod • http://gugod.org • http://twitter.com/gugod • gugod@gugod.org
  3. perl is...
  4. get things done
  5. glue languagee
  6. TIMTOWTDI There is more then one way to do it
  7. the good perl
  8. pragma
  9. pragma = one small english word. Module = title-cased just an convention. Module::Acme pragma
  10. warnings gives you good warning messages
  11. Can anyone tell me if there’s any problem in this small program ? foo.pl #!/usr/bin/perl print $foo; print quot;Helloquot;;
  12. strict
  13. Can any one see a problem in this program ? #!/usr/bin/perl use warnings; print $name; print quot;Helloquot;;
  14. it runs!
  15. (it should break)
  16. $name is undefined
  17. use strict; it breaks your program
  18. in a nice way :-D
  19. feature
  20. Perl 5.10
  21. ← Perl6
  22. use feature;
  23. use feature ‘:5.10’
  24. given - when - default given ($foo) { when (1) { say quot;$foo == 1quot; } when ([2,3]) { say quot;$foo == 2 || $foo == 3quot; } when (/^a[bc]d$/) { say quot;$foo eq 'abd' || $foo eq 'acd'quot; } when ($_ > 100) { say quot;$foo > 100quot; } default { say quot;None of the abovequot; } }
  25. state variables sub counter { state $counts = 0; $counts += 1; }
  26. say say quot;hiquot;;
  27. print quot;hinquot;;
  28. say quot;hiquot;;
  29. use 5.010;
  30. Perl6::* Perl6 functions implemented in Perl5
  31. Perl6::Junctions any, all
  32. Q: How to test if an array contains a specific value ?
  33. Does @ar contains 42 ?
  34. $found = 0; foreach $a (@ar) { if ($a == 42) { $found = 1; last; } } if ($fount) { ... }
  35. if ( grep { $_ == 42 } @ar ) { ... }
  36. if ( grep /^42$/ @ar ) { ... }
  37. use Perl6::Junction qw/ all any none one /;
  38. if ( any(@ar) == 42 ) { ... }
  39. if (all(@ar) > 42) { ... }
  40. if (none(@ar) > 42) { ... }
  41. if (one(@ar) > 42) { ... }
  42. any(values %params) == undef html form validation
  43. any(@birthday) < str2time(quot;1980/01/01quot;)
  44. Can anyone see what it does now ? Can anyone write a nested loop version in 10 seconds ? if ( any(@a) == any(@b) ) { ... }
  45. • Perl6::Junction (any, all) • Perl6::Perl • Perl6::Builtins (system, caller) • Perl6::Form • Perl6::Gather
  46. autobox
  47. my $range = 10−>to(1); # => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
  48. quot;Hello, world!quot;−>uc(); # => quot;HELLO, WORLD!quot;
  49. TryCatch first class try catch semantics
  50. sub foo { eval { # some code that might die return quot;return value from fooquot;; } if ($@) { ... } }
  51. sub foo { try { # some code that might die return quot;return value from fooquot;; } catch (Some::Error $e where { $_->code > 100 } ) { ... } }
  52. Sub::Alias easier function alias
  53. sub name { quot;gugodquot; } alias get_name => 'name'; alias getName => 'name';
  54. self my $self = shift;
  55. package MyClass; sub myMethod { my $self = shift; ... }
  56. package MyClass; use self; sub myMethod { ... }
  57. Moose
  58. Yet-another OO sub-system
  59. EH?
  60. ¿ More ?
  61. OF COURSE
  62. Perl (5) is not like other Object Oriented Languages... does NOT have an OO built-in That's why you should learn perl if you want to learn OO! You can learn how to make an object system, not just how to use it. Dan Kogai
  63. package Point; use Moose; has 'x' => (is => 'rw', isa => 'Int'); has 'y' => (is => 'rw', isa => 'Int'); sub clear { my $self = shift; $self->x(0); $self->y(0); }
  64. MooseX::Declare
  65. class BankAccunt { has 'balance' => ( isa => 'Num', is => 'rw', default => 0 ); method deposit (Num $amount) { $self->balance( $self−>balance + $amount ); } method withdraw (Num $amount) { my $current_balance = $self−>balance(); ( $current_balance >= $amount ) || confess quot;Account overdrawnquot;; $self->balance( $current_balance − $amount ); } }
  66. Rubyish
  67. package Cat; use Rubyish; attr_accessor quot;namequot;, quot;colorquot;; def sound { quot;meow, meowquot; } def speak { print quot;A cat goes quot; . $self−>sound . quot;nquot;; }
  68. the evil perl
  69. prototype
  70. sub doMyWork { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  71. sub doMyWork(@@) { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  72. if (many { $_ > 50 } @arr) { .... }
  73. sub many(&@) { my ($test_sub, @arr) = @_; ... }
  74. AUTOLOAD
  75. sub AUTOLOAD { my $program = $AUTOLOAD; $program =~ s/.*:://; system($program, @_); } date(); who('am', 'i'); ls('−l');
  76. Source Filter
  77. package BANG; use Filter::Simple; FILTER { s/BANGs+BANG!!!/die 'BANG' if $BANG/g; }; 1;
  78. use Acme::Morse; .--.-..--..---.-.--..--.-..--..---.-.--. .-.-........---..-..---.-..-.--..---.--. ..-.---......-...-...-..--..-.-.-.--.-.. ----..-.-.--.-..--..-.-...---.-..---.--. .-...-..--.---...-.-....
  79. Module::Compile
  80. DB inheritable built-in debugger
  81. # from self.pm sub _args { my $level = 2; my @c = (); package DB; @c = caller($level++) while !defined($c[3]) || $c[3] eq '(eval)'; return @DB::args; }
  82. PadWalker runtime stack traveler
  83. sub inc_x { my $h = peek_my(1); ${ $h->{'$x'} }++; }
  84. Binding easier padwalker
  85. use Binding; sub inc_x { my $b = Binding->of_caller; $b->eval('$x + 1'); } sub two { my $x = 1; inc_x; }
  86. Devel::Declare compile-time magician
  87. Compile time code injection
  88. How it works • you define “declarator” keywords • it let compiler stop at the keywords • your code parse the current line in your way, maybe re-write it • you re-place current line with the new version • it resumes the compiler on the current line
  89. def foo($arg1, $arg2) { .... }
  90. def foo($arg1, $arg2) { .... }
  91. def foo($arg1, $arg2) { .... } sub foo { my ($arg1, $arg2) = @_; }
  92. B::Hooks::* more compile time fun
  93. the better perl
  94. to extend perl
  95. the perfect perl
  96. the perfect language?
  97. Perl6 is perfect
  98. The most extendable programming language
  99. • variables • functions, methods • operator overloading
  100. • operators • grammars / rules • sub-language
  101. Perl6 is many languages
  102. Perl6 are many languages
  103. Perl5 world • B::Generate • Source Filter • Devel::Declare
  104. Conclusion
  105. Perl is like the Force. It has a light side, a dark side, and it holds the universe together. Larry Wall
  106. The End Thanks for listening
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×