Good Evils In Perl (Yapc Asia)

5,849 views

Published on

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

No Downloads
Views
Total views
5,849
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

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.
  • Good Evils In Perl (Yapc Asia)

    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. the evil Perl
    68. sub prototype
    69. grep
    70. grep { ... } ...
    71. map
    72. map { ... } ...
    73. sub doMyWork { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
    74. sub doMyWork(@@) { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
    75. many
    76. if (many { $_ > 50 } @arr) { ... }
    77. sub many(&@) { my ($test_sub, @arr) = @_; ... }
    78. AUTOLOAD
    79. sub AUTOLOAD { my $program = $AUTOLOAD; $program =~ s/.*:://; system($program, @_); } date(); who('am', 'i'); ls('−l');
    80. 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. }
    81. Source Filter
    82. package BANG; use Filter::Simple; FILTER { s/BANGs+BANG!!!/die 'BANG' if $BANG/g; }; 1;
    83. use Acme::Morse; .--.-..--..---.-.--..--.-..--..---.-.--. .-.-........---..-..---.-..-.--..---.--. ..-.---......-...-...-..--..-.-.-.--.-.. ----..-.-.--.-..--..-.-...---.-..---.--. .-...-..--.---...-.-....
    84. Module::Compile
    85. perl -MModule::Compile Foo.pm # => Foo.pmc
    86. DB inheritable built-in debugger
    87. # 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] }
    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. 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] }
    90. PadWalker runtime stack traveler
    91. sub inc_x { my $h = peek_my(1); ${ $h->{'$x'} }++; }
    92. Binding easier PadWalker (Rubyish)
    93. use Binding; sub inc_x { my $b = Binding->of_caller; $b->eval('$x + 1'); } sub two { my $x = 1; inc_x; }
    94. Devel::Declare compile-time magician
    95. Devel::Declare compile-time magician Florian Ragwitz id:flora
    96. Compile time code injection
    97. 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
    98. def foo($arg1, $arg2) { .... }
    99. def foo($arg1, $arg2) { .... }
    100. def foo($arg1, $arg2) { .... } sub foo { my ($arg1, $arg2) = @_; }
    101. B::* more compile time fun
    102. Thinking
    103. Perl6 is perfect by Larry Wall youtube: “Larry Wall Speaks at Google”
    104. very3 extensible
    105. Perl6 are many languages
    106. Perl5
    107. very0.5 extensible
    108. • DB • Devel::Declare, B::* • prototype
    109. Good Evil Template::Declare prototype Markapl Devel::Declare self DB, B::* TryCatch Devel::Declare Try::Tiny prototype some Perl6::* source filter
    110. Perl is like the Force. It has a light side, a dark side, and it holds the universe together. Larry Wall
    111. Perl is old
    112. It needs add some “mondern” traits
    113. Extend Perl5 with any “Modern Sense” of a modern programming language.
    114. Optimized for reading
    115. the better perl5
    116. the extendable perl5
    117. PerlX::*
    118. ps: PerlX::Range is lazy!
    119. Thanks for Listening http://youtube.com/gugod/ for cat videos

    ×