Good Evils In Perl

109
Good Evils in Perl Kang-min Liu <[email protected] >

Transcript of Good Evils In Perl

Page 2: Good Evils In Perl

$speaker.meta• 劉康民

Kang-min Liugugod

• http://gugod.org

• http://twitter.com/gugod

[email protected]

• http://handlino.com/和多股份有限公司

Page 3: Good Evils In Perl

perl is...

Page 4: Good Evils In Perl

get things done

Page 5: Good Evils In Perl

glue languagee

Page 6: Good Evils In Perl

TIMTOWTDIThere is more then one way to do it

Page 7: Good Evils In Perl

the good perl

Page 8: Good Evils In Perl

pragma

Page 9: Good Evils In Perl

Module::Acmepragma

pragma = one small english word.

Module = title-cased

just an convention.

Page 10: Good Evils In Perl

warningsgives you good warning messages

Page 11: Good Evils In Perl

#!/usr/bin/perlprint $foo;print "Hello";

Can anyone tell me if there’s any problem in this small program ?

foo.pl

Page 12: Good Evils In Perl

strict

Page 13: Good Evils In Perl

#!/usr/bin/perluse warnings;

print $name;print "Hello";

Can any one see a problem in this program ?

Page 14: Good Evils In Perl

it runs!

Page 15: Good Evils In Perl

(it should break)

Page 16: Good Evils In Perl

$name is undefined

Page 17: Good Evils In Perl

use strict;it breaks your program

Page 18: Good Evils In Perl

in a nice way :-D

Page 19: Good Evils In Perl

feature

Page 20: Good Evils In Perl

Perl 5.10

Page 21: Good Evils In Perl

← Perl6

Page 22: Good Evils In Perl

use feature;

Page 23: Good Evils In Perl

use feature ‘:5.10’

Page 24: Good Evils In Perl

given - when - defaultgiven ($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" }}

Page 25: Good Evils In Perl

state variables

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

Page 26: Good Evils In Perl

say

say "hi";

Page 27: Good Evils In Perl

print "hi\n";

Page 28: Good Evils In Perl

say "hi";

Page 29: Good Evils In Perl

use 5.010;

Page 30: Good Evils In Perl

Perl6::*Perl6 functions implemented in Perl5

Page 31: Good Evils In Perl

Perl6::Junctionsany, all

Page 32: Good Evils In Perl

Q: How to test if an array contains a specific value ?

Page 33: Good Evils In Perl

Does @arcontains 42 ?

Page 34: Good Evils In Perl

$found = 0;foreach $a (@ar) { if ($a == 42) { $found = 1; last; }}if ($fount) { ...}

Page 35: Good Evils In Perl

if ( grep { $_ == 42 } @ar ) { ...}

Page 36: Good Evils In Perl

if ( grep /^42$/ @ar ) { ...}

Page 37: Good Evils In Perl

use Perl6::Junction qw/ all any none one /;

Page 38: Good Evils In Perl

if ( any(@ar) == 42 ) { ...}

Page 39: Good Evils In Perl

if (all(@ar) > 42) { ...}

Page 40: Good Evils In Perl

if (none(@ar) > 42) { ...}

Page 41: Good Evils In Perl

if (one(@ar) > 42) { ...}

Page 42: Good Evils In Perl

any(values %params) == undef

html form validation

Page 43: Good Evils In Perl

any(@birthday) < str2time("1980/01/01")

Page 44: Good Evils In Perl

if ( any(@a) == any(@b) ) { ...}

Can anyone see what it does now ?

Can anyone write a nested loop version in 10 seconds ?

Page 45: Good Evils In Perl

• Perl6::Junction (any, all)

• Perl6::Perl

• Perl6::Builtins (system, caller)

• Perl6::Form

• Perl6::Gather

Page 46: Good Evils In Perl

autobox

Page 47: Good Evils In Perl

my $range = 10−>to(1);# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]

Page 48: Good Evils In Perl

"Hello, world!"−>uc();# => "HELLO, WORLD!"

Page 49: Good Evils In Perl

TryCatch first class try catch semantics

Page 50: Good Evils In Perl

sub foo { eval { # some code that might die return "return value from foo"; } if ($@) { ... }}

Page 51: Good Evils In Perl

sub foo { try { # some code that might die return "return value from foo"; } catch (Some::Error $e where { $_->code > 100 } ) { ... }}

Page 52: Good Evils In Perl

Sub::Aliaseasier function alias

Page 53: Good Evils In Perl

sub name { "gugod" }

alias get_name => 'name';alias getName => 'name';

Page 54: Good Evils In Perl

selfmy $self = shift;

Page 55: Good Evils In Perl

package MyClass;

sub myMethod { my $self = shift; ...}

Page 56: Good Evils In Perl

package MyClass;use self;sub myMethod { ...}

Page 57: Good Evils In Perl

Moose後現代的物件導向系統

Page 58: Good Evils In Perl

Yet-anotherOO sub-system

Page 59: Good Evils In Perl

EH?

Page 60: Good Evils In Perl

¿ More ?

Page 61: Good Evils In Perl

OF COURSE

Page 62: Good Evils In Perl

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

Page 63: Good Evils In Perl

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);}

Page 64: Good Evils In Perl

MooseX::Declare

Page 65: Good Evils In Perl

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 ); }}

Page 66: Good Evils In Perl

Rubyish

Page 67: Good Evils In Perl

package Cat;use Rubyish;

attr_accessor "name", "color";

def sound { "meow, meow" }

def speak { print "A cat goes " . $self−>sound . "\n";}

Page 68: Good Evils In Perl

the evil perl

Page 69: Good Evils In Perl

prototype

Page 70: Good Evils In Perl

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

doMyWork(\@foo, \@bar);

Page 71: Good Evils In Perl

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

doMyWork(@foo, @bar);

Page 72: Good Evils In Perl

if (many { $_ > 50 } @arr) { ....}

Page 73: Good Evils In Perl

sub many(&@) { my ($test_sub, @arr) = @_; ...}

Page 74: Good Evils In Perl

AUTOLOAD

Page 75: Good Evils In Perl
Page 76: Good Evils In Perl

sub AUTOLOAD { my $program = $AUTOLOAD; $program =~ s/.*:://; system($program, @_);}date();who('am', 'i');ls('−l');

Page 77: Good Evils In Perl

Source Filter

Page 78: Good Evils In Perl

package BANG;use Filter::Simple; FILTER { s/BANG\s+BANG!!!/die 'BANG' if \$BANG/g;};

1;

Page 79: Good Evils In Perl

use Acme::Morse;.--.-..--..---.-.--..--.-..--..---.-.--..-.-........---..-..---.-..-.--..---.--...-.---......-...-...-..--..-.-.-.--.-..----..-.-.--.-..--..-.-...---.-..---.--..-...-..--.---...-.-....

Page 80: Good Evils In Perl

Module::Compile

Page 81: Good Evils In Perl

DBinheritable built-in debugger

Page 82: Good Evils In Perl

# from self.pmsub _args { my $level = 2; my @c = (); package DB; @c = caller($level++) while !defined($c[3]) || $c[3] eq '(eval)'; return @DB::args;}

Page 83: Good Evils In Perl

PadWalkerruntime stack traveler

Page 84: Good Evils In Perl

sub inc_x { my $h = peek_my(1); ${ $h->{'$x'} }++;}

Page 85: Good Evils In Perl

Bindingeasier padwalker

Page 86: Good Evils In Perl

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

sub two { my $x = 1; inc_x;}

Page 87: Good Evils In Perl

Devel::Declarecompile-time magician

Page 88: Good Evils In Perl

Compile timecode injection

Page 89: Good Evils In Perl

• 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

How it works

Page 90: Good Evils In Perl

def foo($arg1, $arg2) { ....}

Page 91: Good Evils In Perl

def foo($arg1, $arg2) { ....}

Page 92: Good Evils In Perl

def foo($arg1, $arg2) { ....}

sub foo { my ($arg1, $arg2) = @_;}

Page 93: Good Evils In Perl

B::Hooks::*more compile time fun

Page 94: Good Evils In Perl

the better perl

Page 95: Good Evils In Perl
Page 96: Good Evils In Perl

to extend perl

Page 97: Good Evils In Perl

the perfect perl

Page 98: Good Evils In Perl

the perfect language?

Page 99: Good Evils In Perl

Perl6 is perfect

Page 100: Good Evils In Perl

The most extendable programming language

Page 101: Good Evils In Perl

• variables

• functions, methods

• operator overloading

Page 102: Good Evils In Perl

• operators

• grammars / rules

• sub-language

Page 103: Good Evils In Perl

Perl6 is many languages

Page 104: Good Evils In Perl

Perl6 are many languages

Page 105: Good Evils In Perl

Perl5 world

• B::Generate

• Source Filter

• Devel::Declare

Page 106: Good Evils In Perl

Conclusion

Page 107: Good Evils In Perl

Perl is like the Force. It has a light side, a dark side, and it holds the universe together.

Larry Wall

Page 108: Good Evils In Perl
Page 109: Good Evils In Perl

The EndThanks for listening