Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl 5.16 for the Working Programmer

Perl 5.16 for the Working Programmer

This presentation covers the useful-everyday stuff from Perl 5.16, as well as Perl 5.10 - 5.14.

Ricardo Signes

June 13, 2012
Tweet

More Decks by Ricardo Signes

Other Decks in Programming

Transcript

  1. use feature ‘say’; say “This is a test!”; { no

    feature ‘say’; say “This is fatal!”; }
  2. use 5.16.0; say “This is a test!”; { no feature

    ‘say’; say “This is fatal!”; }
  3. #!/usr/bin/perl use strict; use warnings; use 5.16.0; # use feature

    ‘:5.16’; my $x = Reticulator->new; $x->reticulate(@splines);
  4. #!/usr/bin/perl use strict; use warnings; # no feature; my $x

    = Reticulator->new; $x->reticulate(@splines);
  5. #!/usr/bin/perl use strict; use warnings; # use feature ‘:default’ my

    $x = Reticulator->new; $x->reticulate(@splines);
  6. perldiag $str = “Greetings, $name. Your last login was $last.

    It is now $time.”; Better Error Message(s)
  7. perldiag $str = “Greetings, $name. Your last login was $last.

    It is now $time.”; Better Error Message(s) Use of uninitialized value in concatenation (.) or string at hello.plx line 9.
  8. perldiag Better Error Message(s) Use of uninitialized value $time in

    concatenation (.) or string at hello.plx line 9. $str = “Greetings, $name. Your last login was $last. It is now $time.”;
  9. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; my $price = $amount || $product->price;
  10. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; my $price = $amount || $product->price; ...
  11. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; my $price = $amount || $product->price; ... }
  12. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; $price = defined $amount ? $amount : $product->price; ... }
  13. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; my $price = $amount || $product->price; ... }
  14. perlop truth and definedness sub record_sale { my ($product, $amount)

    = @_; my $price = $amount // $product->price; ... }
  15. perlop the new OR operator sub record_sale { my ($product,

    $amount) = @_; $amount //= $product->cost; ... }
  16. perlfunc - new built-in, say - it’s like print -

    but it adds a newline for you say $what
  17. perlfunc say $what print “Hello, world!\n”; print “$message\n”; print “$_\n”

    for @lines; say “Hello, world!”; say $message; say for @lines;
  18. sub fact { my ($x) = @_; # must be

    +int return $x if $x == 1; return $x * fact($x - 1); } Recursion!
  19. sub fact { my ($x) = @_; # must be

    +int return $x if $x == 1; return $x * fact($x - 1); } Recursion!
  20. my $fact = sub { my ($x) = @_; #

    must be +int return $x if $x == 1; return $x * $fact->($x - 1); }; Recursion!
  21. my $fact = sub { my ($x) = @_; #

    must be +int return $x if $x == 1; return $x * $fact->($x - 1); }; Recursion!
  22. my $fact; $fact = sub { my ($x) = @_;

    # must be +int return $x if $x == 1; return $x * $fact->($x - 1); }; Recursion!
  23. my $fact; $fact = sub { my ($x) = @_;

    # must be +int return $x if $x == 1; return $x * $fact->($x - 1); }; Recursion!
  24. use Scalar::Util qw(weaken); my $fact = do { my $f1;

    my $f2 = $f1 = sub { my ($x) = @_; return $x if $x == 1; return $x * $f1->($x - 1); }; weaken($f1); $f1; }; Recursion!
  25. use 5.16.0; # current_sub my $fact = sub { my

    ($x) = @_; # must be +int return $x if $x == 1; return $x * __SUB__->($x - 1); }; Recursion!
  26. autodie autodie open my $fh, ‘<‘, $filename or die “couldn’t

    open $filename: $!”; while (<$fh>) { ... } close $fh or die “couldn’t close $filename: $!”;
  27. autodie autodie use autodie; open my $fh, ‘<‘, $filename; while

    (<$fh>) { no autodie; rmdir or warn “couldn’t remove $_: $!”; } close $fh;
  28. autodie autodie use autodie; sub foo { my $filename =

    shift; open my $fh, ‘<‘, $filename; while (<$fh>) { ... } } # this implicit close DID NOT AUTODIE
  29. perlopentut IO::File sub stream_to_fh { my ($self, $fh) = @_;

    fileno $fh or die “can’t stream to closed fh”; while (my $hunk = $self->next_hunk) { print {$fh} $hunk; } close $fh or die “error closing: $!”; }
  30. perlopentut IO::File sub stream_to_fh { my ($self, $fh) = @_;

    $fh->fileno or die “can’t stream to closed fh”; while (my $hunk = $self->next_hunk) { $fh->print($hunk); } $fh->close or die “error closing: $!”; }
  31. perlopentut IO::File sub stream_to_fh { ... $fh->print($hunk); ... $fh->close or

    die “error closing: $!”; } open my $target, ‘>’, ‘/dev/null’ or die “can’t open bit bucket: $!”; stream_to_fh($target);
  32. perlopentut IO::File use IO::File; sub stream_to_fh { ... $fh->print($hunk); ...

    $fh->close or die “error closing: $!”; } open my $target, ‘>’, ‘/dev/null’ or die “can’t open bit bucket: $!”; stream_to_fh($target);
  33. perlopentut IO::File use 5.14.0; sub stream_to_fh { ... $fh->print($hunk); ...

    $fh->close or die “error closing: $!”; } open my $target, ‘>’, ‘/dev/null’ or die “can’t open bit bucket: $!”; stream_to_fh($target);
  34. perlopentut IO::File use 5.14.0; use autodie; sub stream_to_fh { ...

    $fh->print($hunk); ... $fh->close or die “error closing: $!”; } open my $target, ‘>’, ‘/dev/null’ or die “can’t open bit bucket: $!”; stream_to_fh($target);
  35. perldoc overloading - the -x overload - the qr overload

    - "no overloading" - unknown overload warns
  36. perldoc smrt match - if $x and $y are unknown,

    there are 23 possible dispatch paths
  37. perldoc smrt match - if $x and $y are unknown,

    there are 23 possible dispatch paths - and some of them redispatch recursively
  38. perldoc smrt match - if $x and $y are unknown,

    there are 23 possible dispatch paths - and some of them redispatch recursively - no, you won't remember them all
  39. perldoc smrt match - if $x and $y are unknown,

    there are 23 possible dispatch paths - and some of them redispatch recursively - no, you won't remember them all - ...and they can't be intuited
  40. if ($x ~~ $y) {...} if ($str ~~ %hash) {...}

    if ($str ~~ @arr) {...} Matching
  41. if ($x ~~ $y) {...} if ($str ~~ %hash) {...}

    if ($str ~~ @arr) {...} if ($str ~~ [ \%h, ...]) {...} Matching
  42. if ($x ~~ $y) {...} if ($str ~~ %hash) {...}

    if ($str ~~ @arr) {...} if ($str ~~ [ \%h, ...]) {...} if (%hash ~~ %h) {...} Matching
  43. if ($x ~~ $y) {...} if ($str ~~ %hash) {...}

    if ($str ~~ @arr) {...} if ($str ~~ [ \%h, ...]) {...} if (%hash ~~ %h) {...} if (%hash ~~ @arr) {...} Matching
  44. if ($x ~~ $y) {...} if ($str ~~ %hash) {...}

    if ($str ~~ @arr) {...} if ($str ~~ [ \%h, ...]) {...} if (%hash ~~ %h) {...} if (%hash ~~ @arr) {...} if (%hash ~~ [ \%h,...]) {...} Matching
  45. given ($x) { when ($y) { try { ... }

    catch { warn “error: $_”; return undef; } } }
  46. ~$ perl5.10.0 -E ‘say scalar localtime 2**31-1’ Mon Jan 18

    22:14:07 2038 ~$ perl5.10.0 -E ‘say scalar localtime 2**31’
  47. ~$ perl5.10.0 -E ‘say scalar localtime 2**31-1’ Mon Jan 18

    22:14:07 2038 ~$ perl5.10.0 -E ‘say scalar localtime 2**31’ Fri Dec 13 15:45:52 1901
  48. ~$ perl5.12.0 -E ‘say scalar localtime 2**31-1’ Mon Jan 18

    22:14:07 2038 ~$ perl5.12.0 -E ‘say scalar localtime 2**31’
  49. ~$ perl5.12.0 -E ‘say scalar localtime 2**31-1’ Mon Jan 18

    22:14:07 2038 ~$ perl5.12.0 -E ‘say scalar localtime 2**31’ Mon Jan 18 22:14:08 2038
  50. Try::Tiny $@ - Well, actually, you use Try::Tiny, right? -

    But this makes Try::Tiny more reliable, too!
  51. Try::Tiny $@ - Well, actually, you use Try::Tiny, right? -

    But this makes Try::Tiny more reliable, too! - You see, eval and $@ are totally awful
  52. perlfunc use 5.12.0; { package X; sub DESTROY { eval

    { } } } eval { my $x = bless {} => ‘X’; die “DEATH!!”; }; warn “ERROR: $@”;
  53. perlfunc use 5.12.0; { package X; sub DESTROY { eval

    { } } } eval { my $x = bless {} => ‘X’; die “DEATH!!”; }; warn “ERROR: $@”; $ perl5.12.4 test.pl ERROR:
  54. perlfunc use 5.14.0; { package X; sub DESTROY { eval

    { } } } eval { my $x = bless {} => ‘X’; die “DEATH!!”; }; warn “ERROR: $@”;
  55. perlfunc use 5.14.0; { package X; sub DESTROY { eval

    { } } } eval { my $x = bless {} => ‘X’; die “DEATH!!”; }; warn “ERROR: $@”; $ perl5.14.1 test.pl ERROR: DEATH!!
  56. perl -le ‘print $^X’ 10.0: perl 10.1: perl 12.0: perl

    14.0: perl 16.0: /Users/rjbs/perl5/perlbrew/perls/16.0/bin/perl
  57. perlunicode Perl 5.16 is Better - Unicode 6.1 - every

    character property is available - \X in regex is more sensible
  58. perlunicode “The Unicode Bug” - strings aren’t always treated as

    Unicode - this causes weird bugs that take ages to find
  59. perlunicode “The Unicode Bug” - strings aren’t always treated as

    Unicode - this causes weird bugs that take ages to find - use feature ‘unicode_strings’;
  60. perlunicode “The Unicode Bug” - strings aren’t always treated as

    Unicode - this causes weird bugs that take ages to find - use feature ‘unicode_strings’; - or use 5.12.0
  61. perldoc Unicode eval - eval $str - is that octets

    or chars? - what if it includes "use utf8" - or you're under "use utf8"?
  62. perldiag My Favorite 5.12-ism? Use of uninitialized value in length

    at - line 3120. if (length $input->{new_email}) { $user->update_email(...); }
  63. perlre qr{ (1) (2) (3) (4) \7 \10 (5) (6)

    (7) (8) (9) \7 \10 (10) \7 \10 }x;
  64. perlre qr{ (1) (2) (3) (4) \o{7} \o{10} (5) (6)

    (7) (8) (9) \o{7} \o{10} (10) \g{7} \g{10} }x;
  65. use 5.16.0; say “I \N{HEAVY BLACK HEART} Queensr” . “\N{LATIN

    SMALL LETTER Y WITH DIAERESIS}” . “che!”; \N{...}
  66. lc ‘ς‘ ➔ ‘ς‘ uc ‘ς‘ ➔ ‘Σ‘ fc ‘ς‘

    ➔ ‘σ‘ lc ‘ß’ ➔ ‘ß’ Case Folding
  67. lc ‘ς‘ ➔ ‘ς‘ uc ‘ς‘ ➔ ‘Σ‘ fc ‘ς‘

    ➔ ‘σ‘ lc ‘ß’ ➔ ‘ß’ uc ‘ß’ ➔ ‘SS’ Case Folding
  68. lc ‘ς‘ ➔ ‘ς‘ uc ‘ς‘ ➔ ‘Σ‘ fc ‘ς‘

    ➔ ‘σ‘ lc ‘ß’ ➔ ‘ß’ uc ‘ß’ ➔ ‘SS’ fc ‘ß’ ➔ ‘ss’ Case Folding
  69. perlre Regex: Named Captures - find matches by name, not

    position - avoid the dreaded $1 - no longer second to Python or .Net!
  70. perlre $line =~ /(\w+):(\w+) = (\w+)/; $section = $1 $name

    = $2; $value = $3; Regex: Named Captures
  71. perlre Regex: Named Captures $line =~ / (?<section> \w+): (?<name>

    \w+) \s* = \s* (?<value> \w+) /x; $section = $+{section}; $name = $+{name}; $value = $+{value};
  72. perlre New Regex Modifiers my @short_names = map { my

    $x = $_; $x =~ s/\..*//s; $x } @long_names;
  73. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  74. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  75. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  76. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  77. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  78. perldoc /u /a /aa /d /l "൮" =~ /\d/ ✓

    ❌ ❌ ¿? ¿? "ð" =~ /\w/ ✓ ❌ ❌ ¿? ¿? "ff" =~ /ff/i ✓ ✓ ❌ ¿? ¿? "ff" =~ /pL/i ✓ ✓ ✓ ¿? ¿? New Regex Modifiers
  79. perlre New Regex Modifiers # To be really ASCII-only: die

    “funny un-American characters” if $str =~ /\P{ASCII}/; $str =~ /...actual pattern.../;
  80. my $re = qr{...complex...}; my $str = q{...long complex...}; $str

    =~ $re; # slow!! study $str; # does stuff study
  81. my $re = qr{...complex...}; my $str = q{...long complex...}; $str

    =~ $re; # slow!! study $str; # does stuff $str =~ $re; # fast!! study
  82. my $re = qr{...complex...}; my $str = q{...long complex...}; $str

    =~ $re; # slow but right!! study $str; # does stuff $str =~ $re; # who knows!! study
  83. my $re = qr{...complex...}; my $str = q{...long complex...}; $str

    =~ $re; # slow but right!! study $str; # does nothing $str =~ $re; # slow but right!! study
  84. $[

  85. perlvar $[ - first index of array - so you

    can make $array[1] mean first
  86. perlvar $[ - first index of array - so you

    can make $array[1] mean first - isn’t that awesome???
  87. perlvar $[ - first index of array - so you

    can make $array[1] mean first - isn’t that awesome??? - yeah, about as awesome as Comic Sans
  88. perlvar $[ Assigned to $[. Are you some kind of

    idiot or something? at -e line 123.