0
Good Evils in Perl
 Kang-min Liu <gugod@gugod.org>
$speaker.meta
•                              •   http://handlino.com/
    Kang-min Liu
    gugod

•   http://gugod.org

• ...
perl is...
get things done
glue languagee
TIMTOWTDI
There is more then one way to do it
the good perl
pragma
pragma = one small
               english word.

               Module = title-cased

               just an convention.

...
warnings
gives you good warning messages
Can anyone tell me if
        there’s any problem in
        this small program ?

        foo.pl




#!/usr/bin/perl
prin...
strict
Can any one see a
                  problem in this program ?




#!/usr/bin/perl
use warnings;

print $name;
print quot;H...
it runs!
(it should break)
$name is undefined
use strict;
it breaks your program
in a nice way :-D
feature
Perl 5.10
← Perl6
use feature;
use feature ‘:5.10’
given - when - default
given ($foo) {
    when (1)            { say quot;$foo == 1quot; }
    when ([2,3])        {
      ...
state variables

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

say quot;hiquot;;
print quot;hinquot;;
say quot;hiquot;;
use 5.010;
Perl6::*
Perl6 functions implemented in Perl5
Perl6::Junctions
      any, all
Q: How to test if an
array contains a specific
value ?
Does @ar
contains 42 ?
$found = 0;
foreach $a (@ar) {
    if ($a == 42) {
        $found = 1;
        last;
    }
}
if ($fount) {
   ...
}
if ( grep { $_ == 42 } @ar ) {
   ...
}
if ( grep /^42$/ @ar ) {
   ...
}
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(@birthday) < str2time(quot;1980/01/01quot;)
Can anyone see what it
                      does now ?

                      Can anyone write a
                      ne...
• 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 ]
quot;Hello, world!quot;−>uc();
# => quot;HELLO, WORLD!quot;
TryCatch
first class try catch semantics
sub foo {
  eval {
    # some code that might die
    return quot;return value from fooquot;;
  }
  if ($@) {
    ...
  }
}
sub foo {
  try {
    # some code that might die
    return quot;return value from fooquot;;
  }
  catch (Some::Error $e w...
Sub::Alias
easier function alias
sub name { quot;gugodquot; }

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
Yet-another
OO sub-system
EH?
¿ More ?
OF COURSE
Perl (5) is not like other Object Oriented Languages... does
NOT have an OO built-in


That's why you should learn perl if...
package Point;
use Moose;

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

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

    method deposit (Num $...
Rubyish
package Cat;
use Rubyish;

attr_accessor quot;namequot;, quot;colorquot;;

def sound { quot;meow, meowquot; }

def speak {...
the evil perl
prototype
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, @ba...
if (many { $_ > 50 } @arr) {
    ....
}
sub many(&@) {
    my ($test_sub, @arr) = @_;
    ...
}
AUTOLOAD
sub AUTOLOAD {
    my $program = $AUTOLOAD;
    $program =~ s/.*:://;
    system($program, @_);
}
date();
who('am', 'i');
...
Source Filter
package BANG;
use Filter::Simple;

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

1;
use Acme::Morse;
.--.-..--..---.-.--..--.-..--..---.-.--.
.-.-........---..-..---.-..-.--..---.--.
..-.---......-...-...-....
Module::Compile
DB
inheritable built-in debugger
# from self.pm
sub _args {
    my $level = 2;
    my @c = ();
    package DB;
    @c = caller($level++)
        while !def...
PadWalker
runtime stack traveler
sub inc_x {
  my $h = peek_my(1);
  ${ $h->{'$x'} }++;
}
Binding
easier padwalker
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
Compile time
code injection
How it works
• you define “declarator” keywords
• it let compiler stop at the keywords
• your code parse the current line i...
def foo($arg1, $arg2) {
    ....
}
def foo($arg1, $arg2) {
    ....
}
def foo($arg1, $arg2) {
      ....
  }



sub foo {
    my ($arg1, $arg2) = @_;
}
B::Hooks::*
more compile time fun
the better perl
to extend perl
the perfect perl
the perfect language?
Perl6 is perfect
The most extendable
programming language
• variables
• functions, methods
• operator overloading
• operators
• grammars / rules
• sub-language
Perl6 is many languages
Perl6 are many languages
Perl5 world

• B::Generate
• Source Filter
• Devel::Declare
Conclusion
Perl is like the Force. It
has a light side, a dark
side, and it holds the
universe together.

             Larry Wall
The End
Thanks for listening
Good Evils In Perl
Good Evils In Perl
Good Evils In Perl
Upcoming SlideShare
Loading in...5
×

Good Evils In Perl

4,070

Published on

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

No Downloads
Views
Total Views
4,070
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

Transcript of "Good Evils In Perl"

  1. 1. Good Evils in Perl Kang-min Liu <gugod@gugod.org>
  2. 2. $speaker.meta • • http://handlino.com/ Kang-min Liu gugod • http://gugod.org • http://twitter.com/gugod • gugod@gugod.org
  3. 3. perl is...
  4. 4. get things done
  5. 5. glue languagee
  6. 6. TIMTOWTDI There is more then one way to do it
  7. 7. the good perl
  8. 8. pragma
  9. 9. pragma = one small english word. Module = title-cased just an convention. Module::Acme pragma
  10. 10. warnings gives you good warning messages
  11. 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. 12. strict
  13. 13. Can any one see a problem in this program ? #!/usr/bin/perl use warnings; print $name; print quot;Helloquot;;
  14. 14. it runs!
  15. 15. (it should break)
  16. 16. $name is undefined
  17. 17. use strict; it breaks your program
  18. 18. in a nice way :-D
  19. 19. feature
  20. 20. Perl 5.10
  21. 21. ← Perl6
  22. 22. use feature;
  23. 23. use feature ‘:5.10’
  24. 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. 25. state variables sub counter { state $counts = 0; $counts += 1; }
  26. 26. say say quot;hiquot;;
  27. 27. print quot;hinquot;;
  28. 28. say quot;hiquot;;
  29. 29. use 5.010;
  30. 30. Perl6::* Perl6 functions implemented in Perl5
  31. 31. Perl6::Junctions any, all
  32. 32. Q: How to test if an array contains a specific value ?
  33. 33. Does @ar contains 42 ?
  34. 34. $found = 0; foreach $a (@ar) { if ($a == 42) { $found = 1; last; } } if ($fount) { ... }
  35. 35. if ( grep { $_ == 42 } @ar ) { ... }
  36. 36. if ( grep /^42$/ @ar ) { ... }
  37. 37. use Perl6::Junction qw/ all any none one /;
  38. 38. if ( any(@ar) == 42 ) { ... }
  39. 39. if (all(@ar) > 42) { ... }
  40. 40. if (none(@ar) > 42) { ... }
  41. 41. if (one(@ar) > 42) { ... }
  42. 42. any(values %params) == undef html form validation
  43. 43. any(@birthday) < str2time(quot;1980/01/01quot;)
  44. 44. Can anyone see what it does now ? Can anyone write a nested loop version in 10 seconds ? if ( any(@a) == any(@b) ) { ... }
  45. 45. • Perl6::Junction (any, all) • Perl6::Perl • Perl6::Builtins (system, caller) • Perl6::Form • Perl6::Gather
  46. 46. autobox
  47. 47. my $range = 10−>to(1); # => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
  48. 48. quot;Hello, world!quot;−>uc(); # => quot;HELLO, WORLD!quot;
  49. 49. TryCatch first class try catch semantics
  50. 50. sub foo { eval { # some code that might die return quot;return value from fooquot;; } if ($@) { ... } }
  51. 51. sub foo { try { # some code that might die return quot;return value from fooquot;; } catch (Some::Error $e where { $_->code > 100 } ) { ... } }
  52. 52. Sub::Alias easier function alias
  53. 53. sub name { quot;gugodquot; } 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
  58. 58. Yet-another OO sub-system
  59. 59. EH?
  60. 60. ¿ More ?
  61. 61. OF COURSE
  62. 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. 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. 64. MooseX::Declare
  65. 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. 66. Rubyish
  67. 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. 68. the evil perl
  69. 69. prototype
  70. 70. sub doMyWork { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  71. 71. sub doMyWork(@@) { my ($arr1, $arr2) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; ... } doMyWork(@foo, @bar);
  72. 72. if (many { $_ > 50 } @arr) { .... }
  73. 73. sub many(&@) { my ($test_sub, @arr) = @_; ... }
  74. 74. AUTOLOAD
  75. 75. sub AUTOLOAD { my $program = $AUTOLOAD; $program =~ s/.*:://; system($program, @_); } date(); who('am', 'i'); ls('−l');
  76. 76. Source Filter
  77. 77. package BANG; use Filter::Simple; FILTER { s/BANGs+BANG!!!/die 'BANG' if $BANG/g; }; 1;
  78. 78. use Acme::Morse; .--.-..--..---.-.--..--.-..--..---.-.--. .-.-........---..-..---.-..-.--..---.--. ..-.---......-...-...-..--..-.-.-.--.-.. ----..-.-.--.-..--..-.-...---.-..---.--. .-...-..--.---...-.-....
  79. 79. Module::Compile
  80. 80. DB inheritable built-in debugger
  81. 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. 82. PadWalker runtime stack traveler
  83. 83. sub inc_x { my $h = peek_my(1); ${ $h->{'$x'} }++; }
  84. 84. Binding easier padwalker
  85. 85. use Binding; sub inc_x { my $b = Binding->of_caller; $b->eval('$x + 1'); } sub two { my $x = 1; inc_x; }
  86. 86. Devel::Declare compile-time magician
  87. 87. Compile time code injection
  88. 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. 89. def foo($arg1, $arg2) { .... }
  90. 90. def foo($arg1, $arg2) { .... }
  91. 91. def foo($arg1, $arg2) { .... } sub foo { my ($arg1, $arg2) = @_; }
  92. 92. B::Hooks::* more compile time fun
  93. 93. the better perl
  94. 94. to extend perl
  95. 95. the perfect perl
  96. 96. the perfect language?
  97. 97. Perl6 is perfect
  98. 98. The most extendable programming language
  99. 99. • variables • functions, methods • operator overloading
  100. 100. • operators • grammars / rules • sub-language
  101. 101. Perl6 is many languages
  102. 102. Perl6 are many languages
  103. 103. Perl5 world • B::Generate • Source Filter • Devel::Declare
  104. 104. Conclusion
  105. 105. Perl is like the Force. It has a light side, a dark side, and it holds the universe together. Larry Wall
  106. 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.

×