Good Evils In Perl

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    8 Favorites & 3 Groups

    Good Evils In Perl - Presentation Transcript

    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 \"Hello\";
    12. strict
    13. Can any one see a problem in this program ? #!/usr/bin/perl use warnings; print $name; print \"Hello\";
    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 \"\\$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\" } }
    25. state variables sub counter { state $counts = 0; $counts += 1; }
    26. say say \"hi\";
    27. print \"hi\\n\";
    28. say \"hi\";
    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(\"1980/01/01\")
    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. \"Hello, world!\"−>uc(); # => \"HELLO, WORLD!\"
    49. TryCatch first class try catch semantics
    50. sub foo { eval { # some code that might die return \"return value from foo\"; } if ($@) { ... } }
    51. sub foo { try { # some code that might die return \"return value from foo\"; } catch (Some::Error $e where { $_->code > 100 } ) { ... } }
    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
    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 \"Account overdrawn\"; $self->balance( $current_balance − $amount ); } }
    66. Rubyish
    67. package Cat; use Rubyish; attr_accessor \"name\", \"color\"; def sound { \"meow, meow\" } def speak { print \"A cat goes \" . $self−>sound . \"\\n\"; }
    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/BANG\\s+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

    + Kang-min LiuKang-min Liu, 6 months ago

    custom

    2149 views, 8 favs, 2 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2149
      • 2122 on SlideShare
      • 27 from embeds
    • Comments 0
    • Favorites 8
    • Downloads 48
    Most viewed embeds
    • 26 views on http://gugod.org
    • 1 views on http://localhost:10010

    more

    All embeds
    • 26 views on http://gugod.org
    • 1 views on http://localhost:10010

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events