0
Good Evils in Perl
Kang-min Liu <gugod@gugod.org>
YAPC::Asia::2009
Perl
get things done
glue language
TIMTOWTDI
There is more then one way to do it
the good Perl
pragma
warnings
gives you good warning messages
Can anyone tell me if
                     there’s any problem in
                     this small program ?

             ...
#!/usr/bin/perl -l

print $foo;
print "Hello";
#!/usr/bin/perl -l
use warnings;
print $foo;
print "Hello";
#!/usr/bin/perl -l
         use warnings;
         print $foo;
         print "Hello";


Use of uninitialized value $foo i...
#!/usr/bin/perl -l
use warnings;
print $foo;
print "Hello";
it runs!
(it should break)
$foo is undeclared
use strict;
it breaks your
program
in a nice way :-D
feature pragma




Perl 5.10
← Perl6
use feature;
use feature ‘:5.10’
use 5.010;
switch
given ($foo) {
   when (1)      { say "$foo == 1" }
   when ([2,3])   {
     say "$foo == 2 || $foo == 3"
   }
   w...
state variables

    sub counter {
      state $counts = 0;
      $counts += 1;
    }
say

      say "hi";
say

      print "hin";
Perl6::*
Perl6 functions implemented in Perl5
Perl6::Junctions
any, all, one, none
Q:
How to test if an
array contains a
specific value ?
Does @a
contains 42 ?
my $found = 0;
for my $a (@a) {
    if ($a == 42) {
        $found = 1;
        last;
    }
}
if ($found) {
   ...
}
if ( grep { $_ == 42 } @a ) {
   ...
}
if ( grep /^42$/ @a ) {
   ...
}
use Perl6::Junction qw/ all any none one /;
if ( any(@ar) == 42 ) {
    ...
}
if ( all(@ar) > 42 ) {
    ...
}
if (none(@ar) > 42 ) {
    ...
}
if ( one(@ar) > 42 ) {
    ...
}
any(values %params) == undef


  html form validation
any(@birthdays) < str2time("1980/01/01")
Can anyone see what it
                         does now ?

                         Can anyone write a
                  ...
• Perl6::Junction (any, all)
• Perl6::Perl
• Perl6::Builtins (system, caller)
• Perl6::Form
• Perl6::Gather
autobox
my $range = 10−>to(1);
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
"Hello, world!"−>uc();
# => "HELLO, WORLD!"
TryCatch
first class try catch semantics
sub foo {
  eval {
    # some code that might die
    return "return value from foo";
  };
  if ($@) {
    ...
  }
}
sub foo {
  try {
    # some code that might die
    return "return value from foo";
  }
  catch (Some::Error $e where { $...
Try::Tiny
minimal
Sub::Alias
easier function alias
sub name { "gugod" }

alias get_name => 'name';
alias getName => 'name';
self
my $self = shift;
package MyClass;


sub myMethod {
  my $self = shift;
  ...
}
package MyClass;
use self;

sub myMethod {

    ...
}
Moose
postmodern OO
package Point;
use Moose;

has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'Int');

sub clear {
  my...
MooseX::Declare
class BankAccunt {
   has 'balance' => (
      isa => 'Num', is => 'rw', default => 0
   );

    method deposit (Num $amou...
Template::Declare
h1 {
  attr { id => "lipsum" };
  outs "Lorem Ipsum";
};

# => <h1 id="lipsum">Lorem Ipsum></h1>
Markapl
h1(id => "lipsum") {
  outs "Lorem Ipsum";
};

# => <h1 id="lipsum">Lorem Ipsum></h1>
Rubyish
package Cat;
use Rubyish;

attr_accessor "name", "color";

def sound { "meow, meow" }

def speak {
  print "A cat goes " ....
☺
the evil Perl
sub prototype
grep
grep { ... } ...
map
map { ... } ...
sub doMyWork {
  my ($arr1, $arr2) = @_;
  my @arr1 = @$arr1;
  my @arr2 = @$arr2;
  ...
}

doMyWork(@foo, @bar);
sub doMyWork(@@) {
  my ($arr1, $arr2) = @_;
  my @arr1 = @$arr1;
  my @arr2 = @$arr2;
  ...
}

doMyWork(@foo, @bar);
many
if (many { $_ > 50 } @arr) {
    ...
}
sub many(&@) {
  my ($test_sub, @arr) = @_;
  ...
}
AUTOLOAD
sub AUTOLOAD {
    my $program = $AUTOLOAD;
    $program =~ s/.*:://;
    system($program, @_);
}
date();
who('am', 'i');
...
Android.pm
sub AUTOLOAD {
  my ($method) = ($AUTOLOAD =~ /::(w+)$/);
  return if $method eq 'DESTROY';
  # print STDERR "$...
Source Filter
package BANG;
use Filter::Simple;

FILTER {
   s/BANGs+BANG!!!/die 'BANG' if $BANG/g;
};

1;
use Acme::Morse;
.--.-..--..---.-.--..--.-..--..---.-.--.
.-.-........---..-..---.-..-.--..---.--.
..-.---......-...-...-....
Module::Compile
perl -MModule::Compile Foo.pm
# => Foo.pmc
DB
inheritable built-in debugger
# from self.pm
sub _args {
   my $level = 2;
   my @c = ();
   package DB;
   @c = caller($level++)
      while !defined($...
# from self.pm
sub _args {
   my $level = 2;
   my @c = ();
   package DB;
   @c = caller($level++)
      while !defined($...
Furthermore, when called from within the DB package, caller
                     returns more detailed information: it set...
PadWalker
runtime stack traveler
sub inc_x {
  my $h = peek_my(1);
  ${ $h->{'$x'} }++;
}
Binding
easier PadWalker (Rubyish)
use Binding;

sub inc_x {
  my $b = Binding->of_caller;
  $b->eval('$x + 1');
}

sub two {
  my $x = 1;
  inc_x;
}
Devel::Declare
compile-time magician
Devel::Declare
compile-time magician




                        Florian Ragwitz
                        id:flora
Compile time
code injection
How it works

• you define “declarator” keywords
• it let compiler stop at the keywords
• your code parse the current line ...
def foo($arg1, $arg2) {
  ....
}
def foo($arg1, $arg2) {
  ....
}
def foo($arg1, $arg2) {
    ....
  }



sub foo {
  my ($arg1, $arg2) = @_;
}
B::*
more compile time fun
Thinking
Perl6 is perfect
by Larry Wall
youtube: “Larry Wall Speaks at Google”
very3   extensible
Perl6 are many
languages
Perl5
very0.5   extensible
• DB
• Devel::Declare, B::*
• prototype
Good                Evil

Template::Declare     prototype

    Markapl         Devel::Declare

      self             DB, ...
Perl is like the
Force. It has a light
side, a dark side,
and it holds the
universe together.
          Larry Wall
Perl is old
It needs add some
“mondern” traits
Extend Perl5 with any
“Modern Sense” of a
modern programming
language.
Optimized for
reading
the better perl5
the extendable
perl5
PerlX::*
ps: PerlX::Range
is lazy!
Thanks for Listening
     http://youtube.com/gugod/ for cat videos
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Upcoming SlideShare
Loading in...5
×

Good Evils In Perl (Yapc Asia)

2,091

Published on

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

No Downloads
Views
Total Views
2,091
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
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 &amp;#x201C;better&amp;#x201D;
  • Think of something for this namespace for your Hackathon, it&amp;#x2019;ll be AWESOME.
  • I just released this lazy range operator module.
  • Transcript of "Good Evils In Perl (Yapc Asia)"

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

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

    ×