Good Evils In Perl (Yapc Asia)
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,309
On Slideshare
3,302
From Embeds
7
Number of Embeds
2

Actions

Shares
Downloads
21
Comments
0
Likes
2

Embeds 7

http://coderwall.com 5
http://www.slideshare.net 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Can any one see a problem in this program ?
  • also checkout the B:: namespaces for goodies
  • with lots of sugars!
  • in whatever definition of “better”
  • Think of something for this namespace for your Hackathon, it’ll be AWESOME.
  • I just released this lazy range operator module.

Transcript

  • 1. Good Evils in Perl Kang-min Liu <gugod@gugod.org> YAPC::Asia::2009
  • 2. Perl
  • 3. get things done
  • 4. glue language
  • 5. TIMTOWTDI There is more then one way to do it
  • 6. the good Perl
  • 7. pragma
  • 8. warnings gives you good warning messages
  • 9. Can anyone tell me if there’s any problem in this small program ? foo.pl #!/usr/bin/perl -l print $foo; print "Hello";
  • 10. #!/usr/bin/perl -l print $foo; print "Hello";
  • 11. #!/usr/bin/perl -l use warnings; print $foo; print "Hello";
  • 12. #!/usr/bin/perl -l use warnings; print $foo; print "Hello"; Use of uninitialized value $foo in print
  • 13. #!/usr/bin/perl -l use warnings; print $foo; print "Hello";
  • 14. it runs!
  • 15. (it should break)
  • 16. $foo is undeclared
  • 17. use strict;
  • 18. it breaks your program
  • 19. in a nice way :-D
  • 20. feature pragma Perl 5.10
  • 21. ← Perl6
  • 22. use feature;
  • 23. use feature ‘:5.10’
  • 24. use 5.010;
  • 25. switch given ($foo) { when (1) { say "$foo == 1" } when ([2,3]) { say "$foo == 2 || $foo == 3" } when (/^a[bc]d$/) { say "$foo eq 'abd' || $foo eq 'acd'" } when ($_ > 100) { say "$foo > 100" } default { say "None of the above" } }
  • 26. state variables sub counter { state $counts = 0; $counts += 1; }
  • 27. say say "hi";
  • 28. say print "hin";
  • 29. Perl6::* Perl6 functions implemented in Perl5
  • 30. Perl6::Junctions any, all, one, none
  • 31. Q: How to test if an array contains a specific value ?
  • 32. Does @a contains 42 ?
  • 33. my $found = 0; for my $a (@a) { if ($a == 42) { $found = 1; last; } } if ($found) { ... }
  • 34. if ( grep { $_ == 42 } @a ) { ... }
  • 35. if ( grep /^42$/ @a ) { ... }
  • 36. use Perl6::Junction qw/ all any none one /;
  • 37. if ( any(@ar) == 42 ) { ... }
  • 38. if ( all(@ar) > 42 ) { ... }
  • 39. if (none(@ar) > 42 ) { ... }
  • 40. if ( one(@ar) > 42 ) { ... }
  • 41. any(values %params) == undef html form validation
  • 42. any(@birthdays) < str2time("1980/01/01")
  • 43. Can anyone see what it does now ? Can anyone write a nested loop version in 10 seconds ? if ( any(@a) == any(@b) ) { ... }
  • 44. • Perl6::Junction (any, all) • Perl6::Perl • Perl6::Builtins (system, caller) • Perl6::Form • Perl6::Gather
  • 45. autobox
  • 46. my $range = 10−>to(1); # => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
  • 47. "Hello, world!"−>uc(); # => "HELLO, WORLD!"
  • 48. TryCatch first class try catch semantics
  • 49. sub foo { eval { # some code that might die return "return value from foo"; }; if ($@) { ... } }
  • 50. sub foo { try { # some code that might die return "return value from foo"; } catch (Some::Error $e where { $_->code > 100 } ) { ... } }
  • 51. Try::Tiny minimal
  • 52. Sub::Alias easier function alias
  • 53. sub name { "gugod" } 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 postmodern OO
  • 58. 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); }
  • 59. MooseX::Declare
  • 60. 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 "Account overdrawn"; $self->balance( $current_balance − $amount ); } }
  • 61. Template::Declare
  • 62. h1 { attr { id => "lipsum" }; outs "Lorem Ipsum"; }; # => <h1 id="lipsum">Lorem Ipsum></h1>
  • 63. Markapl
  • 64. h1(id => "lipsum") { outs "Lorem Ipsum"; }; # => <h1 id="lipsum">Lorem Ipsum></h1>
  • 65. Rubyish
  • 66. package Cat; use Rubyish; attr_accessor "name", "color"; def sound { "meow, meow" } def speak { print "A cat goes " . $self−>sound . "n"; }
  • 67.
  • 68. the evil Perl
  • 69. sub prototype
  • 70. grep
  • 71. grep { ... } ...
  • 72. map
  • 73. map { ... } ...
  • 74. sub doMyWork { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  • 75. sub doMyWork(@@) { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  • 76. many
  • 77. if (many { $_ > 50 } @arr) { ... }
  • 78. sub many(&@) { my ($test_sub, @arr) = @_; ... }
  • 79. AUTOLOAD
  • 80. sub AUTOLOAD { my $program = $AUTOLOAD; $program =~ s/.*:://; system($program, @_); } date(); who('am', 'i'); ls('−l');
  • 81. Android.pm sub AUTOLOAD { my ($method) = ($AUTOLOAD =~ /::(w+)$/); return if $method eq 'DESTROY'; # print STDERR "$0: installing proxy method '$method'n"; my $rpc = rpc_maker($method); { # Install the RPC proxy method, we will not came here # any more for the same method name. no strict 'refs'; *$method = $rpc; } goto &$rpc; # Call the RPC now. }
  • 82. Source Filter
  • 83. package BANG; use Filter::Simple; FILTER { s/BANGs+BANG!!!/die 'BANG' if $BANG/g; }; 1;
  • 84. use Acme::Morse; .--.-..--..---.-.--..--.-..--..---.-.--. .-.-........---..-..---.-..-.--..---.--. ..-.---......-...-...-..--..-.-.-.--.-.. ----..-.-.--.-..--..-.-...---.-..---.--. .-...-..--.---...-.-....
  • 85. Module::Compile
  • 86. perl -MModule::Compile Foo.pm # => Foo.pmc
  • 87. DB inheritable built-in debugger
  • 88. # 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; } sub self { (_args)[0] }
  • 89. # 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; } sub self { (_args)[0] }
  • 90. Furthermore, when called from within the DB package, caller returns more detailed information: it sets the list variable @DB::args to be the arguments with which the subroutine was invoked. # from self.pm – perldoc caller sub _args { my $level = 2; my @c = (); package DB; @c = caller($level++) while !defined($c[3]) || $c[3] eq '(eval)'; return @DB::args; } sub self { (_args)[0] }
  • 91. PadWalker runtime stack traveler
  • 92. sub inc_x { my $h = peek_my(1); ${ $h->{'$x'} }++; }
  • 93. Binding easier PadWalker (Rubyish)
  • 94. use Binding; sub inc_x { my $b = Binding->of_caller; $b->eval('$x + 1'); } sub two { my $x = 1; inc_x; }
  • 95. Devel::Declare compile-time magician
  • 96. Devel::Declare compile-time magician Florian Ragwitz id:flora
  • 97. Compile time code injection
  • 98. 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 replace current line with the new one • resumes the compiler on the current line
  • 99. def foo($arg1, $arg2) { .... }
  • 100. def foo($arg1, $arg2) { .... }
  • 101. def foo($arg1, $arg2) { .... } sub foo { my ($arg1, $arg2) = @_; }
  • 102. B::* more compile time fun
  • 103. Thinking
  • 104. Perl6 is perfect by Larry Wall youtube: “Larry Wall Speaks at Google”
  • 105. very3 extensible
  • 106. Perl6 are many languages
  • 107. Perl5
  • 108. very0.5 extensible
  • 109. • DB • Devel::Declare, B::* • prototype
  • 110. Good Evil Template::Declare prototype Markapl Devel::Declare self DB, B::* TryCatch Devel::Declare Try::Tiny prototype some Perl6::* source filter
  • 111. Perl is like the Force. It has a light side, a dark side, and it holds the universe together. Larry Wall
  • 112. Perl is old
  • 113. It needs add some “mondern” traits
  • 114. Extend Perl5 with any “Modern Sense” of a modern programming language.
  • 115. Optimized for reading
  • 116. the better perl5
  • 117. the extendable perl5
  • 118. PerlX::*
  • 119. ps: PerlX::Range is lazy!
  • 120. Thanks for Listening http://youtube.com/gugod/ for cat videos