+ All Categories
Home > Documents > Perl 510 for People Who Arent Totally Insane 4609

Perl 510 for People Who Arent Totally Insane 4609

Date post: 09-Apr-2018
Category:
Upload: sagi123
View: 216 times
Download: 0 times
Share this document with a friend

of 184

Transcript
  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    1/184

    Perl 5.10insanefor people who are not

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    2/184

    Perl 5.10insanetotallyfor people who are not

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    3/184

    perl5100delta

    5.10 isnt like 5.8.x

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    4/184

    perl5100delta

    5.10 isnt like 5.8.x

    -features only get added in new 5.x releases

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    5/184

    perl5100delta

    5.10 isnt like 5.8.x

    -features only get added in new 5.x releases

    - its been 5 years since the last release (5.8)

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    6/184

    perl51000delta

    5.10 is Way Cool

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    7/184perl51000delta

    5.10 is Way Cool

    - no significant new features in Perl since 2002

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    8/184perl51000delta

    5.10 is Way Cool

    - no significant new features in Perl since 2002

    -

    remember how bad you wanted to see Star

    Wars: Episode 1?

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    9/184perl51000delta

    5.10 is Way Cool

    - no significant new features in Perl since 2002

    -

    remember how bad you wanted to see Star

    Wars: Episode 1?

    - thats how excited you should be for 5.10

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    10/184perl51000delta

    5.10 is Way Cool

    - no significant new features in Perl since 2002

    -

    remember how bad you wanted to see Star

    Wars: Episode 1?

    - thats how excited you should be for 5.10

    -but it wont suck (no POD race scene)

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    11/184perl51000delta

    Lexicascopasmartwhat?

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    12/184perl51000delta

    Lexicascopasmartwhat?

    - lexically scoped user pragmata!

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    13/184perl51000delta

    Lexicascopasmartwhat?

    - lexically scoped user pragmata!

    -pluggable regex compilation engines!

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    14/184perl51000delta

    Lexicascopasmartwhat?

    - lexically scoped user pragmata!

    -pluggable regex compilation engines!

    - trie-based non-recursive pattern matching!

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    15/184

    perl51000delta

    Lexicascopasmartwhat?

    - lexically scoped user pragmata!

    -pluggable regex compilation engines!

    - trie-based non-recursive pattern matching!

    -

    thread-safe weak refkey hashes!

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    16/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    17/184

    perl51000delta

    Yes, You Care

    - Not everything in 5.10 is esoteric.

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    18/184

    perl51000delta

    Yes, You Care

    - Not everything in 5.10 is esoteric.

    -Not everything in 5.10 is for gurus.

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    19/184

    perl51000delta

    Yes, You Care

    - Not everything in 5.10 is esoteric.

    -Not everything in 5.10 is for gurus.

    - Not everything in 5.10 is for C programmers.

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    20/184

    perl51000delta

    Yes, You Care

    - Not everything in 5.10 is esoteric.

    -Not everything in 5.10 is for gurus.

    - Not everything in 5.10 is for C programmers.

    -

    Not everything in 5.10 is super advanced.

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    21/184

    feature

    First: A Warning

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    22/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    23/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    24/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    25/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

    - use feature mtfnpy;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    26/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

    - use feature mtfnpy;

    -

    use 5.010;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    27/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

    - use feature mtfnpy;

    -

    use 5.010;

    - read the perldoc

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    28/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

    - use feature mtfnpy;

    -

    use 5.010;

    - read the perldoc Im a per ldocref!

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    29/184

    feature

    First: A Warning

    - 5.10 is backwards compatible

    - but adds new keywords and operators

    -theyre not enabled by default

    - use feature mtfnpy;

    -

    use 5.010;

    - read the perldoc

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    30/184

    say what

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    31/184

    perlfunc

    -

    new built-in, say

    - its like print

    - but it adds a newline for you

    say $what

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    32/184

    perlfunc

    say $what

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    33/184

    perlfunc

    say $what

    print Hello, world!\n;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    34/184

    perlfunc

    say $what

    print Hello, world!\n;

    print $message\n;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    35/184

    perlfunc

    say $what

    print Hello, world!\n;

    print $message\n;

    print $_\n for @lines;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    36/184

    perlfunc

    say $what

    print Hello, world!\n;

    print $message\n;

    print $_\n for @lines;

    say Hello, world!;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    37/184

    perlfunc

    say $what

    print Hello, world!\n;

    print $message\n;

    print $_\n for @lines;

    say Hello, world!;

    say $message;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    38/184

    perlfunc

    say $what

    print Hello, world!\n;

    print $message\n;

    print $_\n for @lines;

    say Hello, world!;

    say $message;

    say for @lines;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    39/184

    truth and definedness

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    40/184

    perlop

    truth and definedness

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    41/184

    perlop

    truth and definedness

    sub record_sale {

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    42/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    43/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount ||= $product->cost;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    44/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount ||= $product->cost;

    ...

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    45/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount ||= $product->cost;

    ...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    46/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount = defined $amount? $amount: $product->cost;

    ...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    47/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount ||= $product->cost;

    ...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    48/184

    perlop

    truth and definedness

    sub record_sale {my ($product, $amount) = @_;

    $amount ||= $product->cost;

    ...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    49/184

    perlop

    the new OR operator

    sub record_sale {my ($product, $amount) = @_;

    $amount //= $product->cost;

    ...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    50/184

    perlop

    the new OR operator

    $setting = defined $given? $given: $default;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    51/184

    perlop

    the new OR operator

    $setting = $given;unless (defined $setting) {$setting = $default;

    }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    52/184

    perlop

    the new OR operator

    $setting = $given || $default;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    53/184

    perlop

    the new OR operator

    $setting = $given // $default;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    54/184

    keeping state

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    55/184

    perlsub

    $lines_left = 100;

    sub read_line {die trial period expiredunless $lines_left-- > 0;

    ...}

    State Variables

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    56/184

    perlsub

    my $lines_left = 100;

    sub read_line {die trial period expiredunless $lines_left-- > 0;

    ...}

    State Variables

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    57/184

    perlsub

    {my $lines_left = 100;

    sub read_line {die trial period expiredunless $lines_left-- > 0;

    ...}}

    State Variables

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    58/184

    perlsub

    package Trial::Period;sub new {my ($class, $arg) = @_;my $guts = {lines_left => $arg->{lines},error_msg => $arg->{error},

    };return bless $guts => $class;

    }

    sub consume_line {my ($self) = @_;$self->{lines_left}--;

    }

    sub lines_left {my ($self) = @_;return $self->{lines_left};

    }

    sub assert_lines_left {my ($self) = @_;unless ($self->lines_left) {die $self->{error_msg};

    }}1;

    State Variables

    my $LINES = 100;my $ERROR = sorry, trial period over;my $TRIAL = Trial::Period->new({lines => $LINES,error => $ERROR,

    });

    sub read_line {$TRIAL->assert_lines_left;...

    }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    59/184

    perlsub

    {my $lines_left = 100;

    sub read_line {die trial period expiredunless $lines_left-- > 0;

    ...}}

    State Variables

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    60/184

    perlsub

    sub read_line {

    state $lines_left = 100;die trial period expiredunless $lines_left-- > 0;

    ...}

    State Variables

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    61/184

    -x stacking

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    62/184

    perlfunc

    if (-f $fileand -w $fileand -z $file

    ) {

    unlink $file;}

    Stackable File Tests

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    63/184

    perlfunc

    if (-f $file and -w _ and -z _

    ) {unlink $file;

    }

    Stackable File Tests

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    64/184

    perlfunc

    if (-f -w -z $file) {unlink $file;

    }

    Stackable File Tests

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    65/184

    smart matching

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    66/184

    perlsyn

    Smart Matching

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    67/184

    perlsyn

    Smart Matching

    - a new kind of comparison operator

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    68/184

    perlsyn

    Smart Matching

    - a new kind of comparison operator

    -its behavior depends on its inputs

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    69/184

    perlsyn

    Smart Matching

    - a new kind of comparison operator

    -its behavior depends on its inputs

    - if these two things match...

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    70/184

    perlsyn

    Smart Matching

    - a new kind of comparison operator

    -its behavior depends on its inputs

    - if these two things match...

    -

    hard to tell, easy to show...

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    71/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    72/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    73/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    74/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    75/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    76/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    77/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }

    elsif ($foo ~~ $bar) { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    78/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }

    elsif ($foo ~~ $bar) { ... }else { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    79/184

    perlsyn

    Smart Matching

    given ($foo) {when (undef) { ... }when (@array) { ... }when ($code) { ... }when (%hash) { ... }when (qr/re/) { ... }

    when ($bar) { ... }default { ... }}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    80/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }

    elsif ($foo ~~ $bar) { ... }else { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    81/184

    perlsyn

    Smart Matching

    if ($foo ~~ undef) { ... }elsif ($foo ~~ $array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ $hash) { ... }elsif ($foo ~~ qr/re/) { ... }

    elsif ($foo ~~ $bar) { ... }else { ... }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    82/184

    perlsyn

    Smart Matching

    my $test;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    83/184

    perlsyn

    Smart Matching

    given ($foo) {when (undef) { ... }when ($aref) { ... }when ($code) { ... }when ($href) { ... }when ($regex) { ... }

    when ($object) { ... }default { ... }}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    84/184

    perlsyn

    Smart Matching

    given ($foo) {when ($test_1) { ... }when ($test_2) { ... }when ($test_3) { ... }when ($test_4) { ... }when ($test_5) { ... }

    when ($test_6) { ... }default { ... }}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    85/184

    perlsyn

    Smart Matching

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    86/184

    perlsyn

    Smart Matching

    @want = @have->where($test)

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    87/184

    perlsyn

    Smart Matching

    @want = @have->where($test)

    @want = @have->where(sub{ })

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    88/184

    h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    89/184

    perlsyn

    Smart Matching

    @want = @have->where($test)

    @want = @have->where(sub{ })

    @want = @have->where(qr/.../sm)

    @want = @have->where([ 1,2,3 ])

    h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    90/184

    perlsyn

    Smart Matching

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    91/184

    S M h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    92/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    93/184

    S M h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    94/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    S M h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    95/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;

    S M h

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    96/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    97/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    98/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    99/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    100/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }if (ref $test eq CODE) {

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    101/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }if (ref $test eq CODE) {return grep { $test->($_) } @$array;

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    102/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }if (ref $test eq CODE) {return grep { $test->($_) } @$array;

    }

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    103/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }if (ref $test eq CODE) {return grep { $test->($_) } @$array;

    }

    die invalid test

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    104/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;

    if (ref $test eq ARRAY) {my %known = map {$_=>1} @$test;

    return grep { $known{$_} } @$array;}if (ref $test eq Regexp) {return grep { $_ =~ $test } @$array;

    }if (ref $test eq CODE) {return grep { $test->($_) } @$array;

    }

    die invalid test}

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    105/184

    perlsyn

    Smart Matching

    sub where {my ($array, $test) = @_;grep { $_ ~~ $test } @$array;

    }

    S M hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    106/184

    perlsyn

    Smart Matching

    SmartMatch::Sugar

    S t M t hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    107/184

    perlsyn

    Smart Matching

    @want = @have->where( hash )

    SmartMatch::Sugar

    S t M t hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    108/184

    perlsyn

    Smart Matching

    @want = @have->where( hash )

    @want = @have->where( class )

    SmartMatch::Sugar

    S t M t hi

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    109/184

    perlsyn

    Smart Matching

    @want = @have->where( hash )

    @want = @have->where( class )

    @want = @have->where(isa(Foo))

    SmartMatch::Sugar

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    110/184

    unknown undefined

    B tt E M ( )

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    111/184

    perldiag

    $str = Greetings, $name. Your lastlogin was $last. It is now $time.;

    Better Error Message(s)

    B tt E M ( )

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    112/184

    perldiag

    $str = Greetings, $name. Your lastlogin was $last. It is now $time.;

    Better Error Message(s)

    Use of uninitialized value in

    concatenation (.) or string athello.plx line 9.

    B tt E M g ( )

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    113/184

    perldiag

    Better Error Message(s)

    Use of uninitialized value $time in

    concatenation (.) or string athello.plx line 9.

    $str = Greetings, $name. Your lastlogin was $last. It is now $time.;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    114/184

    inside-out objects

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    115/184

    Hash::Util::FieldHash

    Inside-Out Objects

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    116/184

    Hash::Util::FieldHash

    Inside-Out Objects

    - traditional objects are a hashref

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    117/184

    Hash::Util::FieldHash

    Inside-Out Objects

    - traditional objects are a hashref

    -

    look up attrs in the obj itself by attr name

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    118/184

    Hash::Util::FieldHash

    Inside-Out Objects

    - traditional objects are a hashref

    -

    look up attrs in the obj itself by attr name- inside-out objects are content-free refs

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    119/184

    Hash::Util::FieldHash

    Inside-Out Objects

    - traditional objects are a hashref

    -

    look up attrs in the obj itself by attr name- inside-out objects are content-free refs

    - look up attrs in an external hash by obj id

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    120/184

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    121/184

    Hash::Util::FieldHash

    Inside-Out Objects

    sub size {my $self = shift;

    if (@_) {return $self->{size} = shift;

    } else {return $self->{size};

    }}

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    122/184

    Hash::Util::FieldHash

    Inside-Out Objects

    my %size;sub size {

    my $self = shift;

    if (@_) {return $size{ $self } = shift;

    } else {

    return $size{ $self };}}

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    123/184

    Hash::Util::FieldHash

    Inside-Out Objects

    my %size;sub size {my $self = shift;my $id = refaddr $self;

    if (@_) {return $size{ $id } = shift;

    } else {

    return $size{ $id };}

    }

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    124/184

    Inside Out Objects

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    125/184

    Hash::Util::FieldHash

    Inside-Out Objectsmy %size;sub size {my $self = shift;my $id = refaddr $self;

    if (@_) {return $size{ $id } = shift;

    } else {return $size{ $id };

    }}

    sub DESTROY {my $id = refaddr $_[0];delete $size{ $id };

    }

    sub CLONE {my $class = shift;

    my @properties = map { values %$_ }values %PROP_DATA_FOR;

    for my $old_id ( keys %OBJ ) {

    my $object = $OBJ{ $old_id };my $new_id = refaddr $object;

    for my $prop ( @properties ) {next unless exists $prop->{ $old };$prop->{ $new } = $prop->{ $old };delete $prop->{ $old };

    }

    weaken ( $OBJ{ $new } = $object );delete $OBJ{ $old };

    }}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    126/184

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    127/184

    Hash::Util::FieldHash

    Field Hashes

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    128/184

    Hash::Util::FieldHash

    Field Hashes

    - theyre just like hashes

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    129/184

    Hash::Util::FieldHash

    Field Hashes

    - theyre just like hashes

    - objects as keys become object ids

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    130/184

    Hash::Util::FieldHash

    Field Hashes

    - theyre just like hashes

    - objects as keys become object ids

    - but get destroyed and cloned

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    131/184

    Hash::Util::FieldHash

    Field Hashes

    my %size;sub size {

    my $self = shift;

    if (@_) {return $size{ $self } = shift;

    } else {

    return $size{ $self };}}

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    132/184

    Hash::Util::FieldHash

    Field Hashes

    fieldhash my %size;sub size {

    my $self = shift;

    if (@_) {return $size{ $self } = shift;

    } else {

    return $size{ $self };}}

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    133/184

    Hash::Util::FieldHash

    Field Hashes

    {fieldhash my %size;sub size {my $self = shift;if (@_) {return $size{ $self } = shift;} else {return $size{ $self };

    }}

    }

    Field Hashes

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    134/184

    Hash::Util::FieldHash

    Field Hashes

    sub size {my $self = shift;fieldhash state %size;

    if (@_) {return $size{ $self } = shift;

    } else {

    return $size{ $self };}}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    135/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    136/184

    Lexical Topic

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    137/184

    perlvar

    for (@lines) {chomp;

    next if /^#/;next unless length;s/a/b/;sanity_check;

    say;}

    Lexical Topic

    Lexical Topic

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    138/184

    perlvar

    Lexical Topic

    for my $line (@lines) {chomp $line;

    next if $line ~~ /^#/;next unless length $line;$line =~ s/a/b/;sanity_check($line);

    say $line;}

    Lexical Topic

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    139/184

    perlvar

    for my $_ (@lines) {chomp;

    next if /^#/;next unless length;s/a/b/;sanity_check;

    say;}

    Lexical Topic

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    140/184

    Regex

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    141/184

    named captures

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    142/184

    perlre

    Regex: Named Captures

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    143/184

    perlre

    Regex: Named Captures

    - find matches by name, not position

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    144/184

    perlre

    Regex: Named Captures

    - find matches by name, not position

    - avoid the dreaded $1

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    145/184

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    146/184

    perlre

    # our hypothetical format

    section:property = value

    Regex: Named Captures

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    147/184

    perlre

    $line ~~ /(\w+):(\w+) = (\w+)/;$name = $2;$value = $3;

    Regex: Named Captures

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    148/184

    perlre

    $lhs_re = qr/(\w+):(\w+)/;$rhs_re = qr/(\w+)/;

    $line ~~ /$lhs_re = $rhs_re/;

    $name = $2;

    $value = $3;

    Regex: Named Captures

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    149/184

    perlre

    $line ~~ /$lhs_re = $rhs_re/;$name = $2;$value = $3;

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    150/184

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    151/184

    perlre

    $lhs_re = qr/(\w+):(\w+)/;$rhs_re = qr/(\w+)/;

    g p

    Regex: Named Captures

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    152/184

    perlre

    $lhs_re = qr/(\w+):(?\w+)/;$rhs_re = qr/(?\w+)/;

    g p

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    153/184

    better backrefs

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    154/184

    perlre

    g

    m{< (\w+) > .+ < /\1 > }x

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    155/184

    perlre

    g

    m{< (\w+) > .+ < /\1 > }x

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    156/184

    perlre

    g

    \10

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    157/184

    perlre

    g

    qr{(\d)\10}

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    158/184

    perlre

    g

    qr{ (\d) \1 0}x

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    159/184

    perlre

    g

    m{< (\w+) > .+ < /\1 > }x

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    160/184

    perlre

    g

    m{< (\w+) > .+ < /\g{1} > }x

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    161/184

    perlre

    g

    \10

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    162/184

    perlre

    g

    \g{10}

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    163/184

    perlre

    g

    qr{(\d)\10}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    164/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    165/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    166/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    167/184

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    168/184

    perlop

    if ($str =~ $wtf_re) {...}

    Regex: Backreference

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    169/184

    perlop

    if ($str ~~ $wtf_re) {...}

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    170/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    171/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    172/184

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    173/184

    p rlr pi

    -

    lets you change how regex work

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    174/184

    p l pi

    -

    lets you change how regex work

    - but not how you use them (=~, s///, etc)

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    175/184

    gi POSIX

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    176/184

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    177/184

    re::engine::POSIX

    my $regex = qr{ ... };

    my @files = $find->file->in( $root );

    say for grep { slurp ~~ $re } @files;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    178/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    179/184

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    180/184

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    181/184

    re::engine::POSIX

    use re::engine::POSIX;

    my $regex = qr{ ... };

    my @files = $find->file->in( $root );

    say for grep { slurp ~~ $re } @files;

    Alternate Regex Engines

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    182/184

    re::engine::POSIX

    use re::engine::POSIX;

    my $regex = qr{ ... }x;

    my @files = $find->file->in( $root );

    say for grep { slurp ~~ $re } @files;

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    183/184

    http://www.perl.org/get.html

  • 8/8/2019 Perl 510 for People Who Arent Totally Insane 4609

    184/184


Recommended