SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
Modern Core Perl

        Dave Cross
   Magnum Solutions Ltd
    dave@mag-sol.com
What We Will Cover
          Recent Perl releases
          5.10
          5.12
          5.14
          5.16


                                     2
London Perl Workshop
 12th November 2011
Perl Releases
          Perl 5 has moved to a regular release cycle
          Major release every year
             −   In Spring
          Minor releases when required




                                                         3
London Perl Workshop
 12th November 2011
Perl Version Numbers
          Even major numbers are production releases
             −   5.10, 5.12, 5.14
          Odd major numbers are dev releases
             −   5.9, 5.11, 5.13




                                                        4
London Perl Workshop
 12th November 2011
Perl Support
          p5p provide support for current and previous
           major releases
             −   Currently 5.12 and 5.14
          Further support may be available from
           distributors




                                                          5
London Perl Workshop
 12th November 2011
Recent Perl Releases
          5.10.0 – 2007 Dec 18
          5.10.1 – 2009 Aug 22
          5.12.0 – 2010 Apr 12
          5.12.1 – 2010 May 16
          5.12.2 – 2010 Sep 6
          5.12.3 – 2011 Jan 21

                                    6
London Perl Workshop
 12th November 2011
Recent Perl Releases
          5.14.0 – 2011 May 14
          5.14.1 – 2011 Jun 16
          5.12.4 – 2011 Jun 20
          5.14.2 – 2011 Sep 26




                                    7
London Perl Workshop
 12th November 2011
Perl 5.10
          Released 18th Dec 2007
             −   Perl's 20th birthday
          Many new features
          Well worth upgrading




                                        8
London Perl Workshop
 12th November 2011
New Features
          Defined-or operator
          Switch operator
          Smart matching
          say()
          Lexical $_


                                      9
London Perl Workshop
 12th November 2011
New Features
          State variables
          Stacked file tests
          Regex improvements
          Many more




                                      10
London Perl Workshop
 12th November 2011
Defined Or
          Boolean expressions “short-circuit”
          $val = $val || $default;
          $val ||= $default;
          What if 0 is a valid value?




                                                 11
London Perl Workshop
 12th November 2011
Defined Or
          Need to check “definedness”
          $val = defined $val
                       ? $val : $default;
          $val = $default
             unless defined $val;



                                            12
London Perl Workshop
 12th November 2011
Defined Or
          The defined or operator makes this easier
          $val = $val // $default;
          A different slant on truth
          Checks definedness
          Shortcircuit version too
          $val //= $value;

                                                       13
London Perl Workshop
 12th November 2011
Switch Statement
          Switch.pm was added with Perl 5.8
          Source filter
          Parser limitations
             −   Regular expressions
             −   eval
          5.10 introduces a build-in switch statement

                                                         14
London Perl Workshop
 12th November 2011
Given ... When
          Switch is spelled “given”
          Case is spelled “when”
          Powerful matching syntax




                                        15
London Perl Workshop
 12th November 2011
Given Example
          given ($foo) {
               when (/^abc/) { $abc   = 1; }
               when (/^def/) { $def   = 1; }
               when (/^xyz/) { $xyz   = 1; }
               default { $nothing =   1; }
           }




                                               16
London Perl Workshop
 12th November 2011
New Keywords
          Four new keywords
             −   given
             −   when
             −   default
             −   continue




                                      17
London Perl Workshop
 12th November 2011
given
          given(EXPR)
          Assigns the result of EXPR to $_ within the
           following block
          Similar to do { my $_ = EXPR; ... }




                                                         18
London Perl Workshop
 12th November 2011
when
          when (EXPR)
          Uses smart matching to compare $_ with
           EXPR
          Equivalent to when ($_ ~~ EXPR)
          ~~ is the new smart match operator
          Compares two values and “does the right
           thing”
                                                     19
London Perl Workshop
 12th November 2011
default
          default defines a block that is executed if no
           when blocks match
          default block is optional




                                                            20
London Perl Workshop
 12th November 2011
continue
          continue keyword falls through to the next
           when block
          Normal behaviour is to break out of given
           block once the first when condition is
           matched
          Inverse of most other programming
           languages

                                                        21
London Perl Workshop
 12th November 2011
continue
          given($foo) {
             when (/x/)
               { say '$foo contains an x';
                 continue }
             when (/y/)
               { say '$foo contains a y' }
             default
               { say '$foo contains no x or y' }
           }



                                                   22
London Perl Workshop
 12th November 2011
Smart Matching
          ~~ is the new Smart Match operator
          Different kinds of matches
          Dependent on the types of the operands
          See “perldoc perlsyn” for the full details
          Warning: Still under discussion


                                                        23
London Perl Workshop
 12th November 2011
Smart Match Examples
          $foo ~~ $bar; # == or cmp
          @foo ~~ $bar; # array contains value
          %foo ~~ $bar; # hash key exists
          $foo ~~ qr{$bar}; # regex match
          @foo ~~ @bar; # arrays are identical
          %foo ~~ %bar; # hash keys match
          Many more alternatives
                                                  24
London Perl Workshop
 12th November 2011
say()
          say() is a new alternative to print()
          Adds a new line at the end of each call
          say($foo); # print $foo, “n”;
          Two characters shorter than print
          Less typing


                                                     25
London Perl Workshop
 12th November 2011
Lexical $_
          $_ is a package variable
          Always exists in main package
          Can lead to subtle bugs when not localised
           correctly
          Can now use my $_ to create a lexically
           scoped variable called $_


                                                        26
London Perl Workshop
 12th November 2011
State Variables
          Lexical variables disappear when their scope
           is destroyed
          sub variables {
               my $x;

                 say ++$x;
           }

           variables() for 1 .. 3;
                                                          27
London Perl Workshop
 12th November 2011
State Variables
          State variables retain their value when their
           scope is destroyed
          sub variables {
              state $x;

                say ++$x;
           }

           variables() for 1 .. 3;
                                                           28
London Perl Workshop
 12th November 2011
State Variables
          Like static variables in C
          Deprecating bugs
             −   my $x if 0;




                                         29
London Perl Workshop
 12th November 2011
Stacked File Tests
          People often think you can do this
          -f -w -x $file
          Previously you couldn't
          Now you can
          Equivalent to
          -x $file && -w _ && -f _

                                                30
London Perl Workshop
 12th November 2011
Regex Improvements
          Plenty of regular expression improvements
          Named capture buffers
          Possessive quantifiers
          Relative backreferences
          New escape sequences
          Many more

                                                       31
London Perl Workshop
 12th November 2011
Named Capture Buffers
          Variables $1, $2, etc change if the regex is
           altered
          Named captures retain their names
          (?<name> ... ) to define
          Use new %+ hash to access them



                                                          32
London Perl Workshop
 12th November 2011
Named Capture Example
          while (<DATA>) {
             if (/(?<header>[ws]+)
                 :s+(?<value>.+)/x) {
               print "$+{header} -> ";
               print "$+{value}n";
             }
           }


                                         33
London Perl Workshop
 12th November 2011
Possessive Quantifiers
          ?+, *+, ++
          Grab as much as they can
          Never give it back
          Finer control over backtracking
          'aaaa' =~ /a++a/
          Never matches

                                             34
London Perl Workshop
 12th November 2011
Relative Backreferences
          g{N}
          More powerful version of 1, 2, etc
          g{1} is the same as 1
          g{-1} is the last capture buffer
          g{-2} is the one before that


                                                  35
London Perl Workshop
 12th November 2011
New Escape Sequences
          h – Horizontal white space
          v – Vertical white space
          Also H and V




                                         36
London Perl Workshop
 12th November 2011
Accessing New Features
          Some new features would break backwards
           compatibility
          They are therefore turned off by default
          Various ways to turn them on




                                                      37
London Perl Workshop
 12th November 2011
Feature Pragma
          Turn new features on with the feature
           pragma
          use feature 'say';
          use feature 'switch';
          use feature 'state';
          use feature ':5.10';



                                                   38
London Perl Workshop
 12th November 2011
Implicit Loading
          Two ways to automatically turn on 5.10
           features
          Require a high enough version of Perl
          use 5.10.0; # Or higher
          -E command line option
          perl -e 'say “hello”'
          perl -E 'say “hello”'
                                                    39
London Perl Workshop
 12th November 2011
Perl 5.12
          Released 12 April 2010
             −   5.12.4 20 June 2011
          Many new enhancements




                                        40
London Perl Workshop
 12th November 2011
5.12 Enhancements
          package NAME VERSION syntax
          ... operator
          Implicit strictures
          Y2038 compliance




                                         41
London Perl Workshop
 12th November 2011
5.12 Enhancements
          Smart match changes
          New modules
             −   autodie
             −   parent




                                     42
London Perl Workshop
 12th November 2011
package NAME VER
          Declare the version of a package in the
           package declaration
          package My::Package 1.23;
          Equivalent to
          package My::Package;
           our $VERSION = 1.23;


                                                     43
London Perl Workshop
 12th November 2011
... Operator
          Called the “yada-yada” operator
          Used to stand in for unwritten code
          sub unimplemented {
             ...
           }
          Code compiles
          Throws an “unimplemented” exception
           when run
                                                 44
London Perl Workshop
 12th November 2011
Implicit Strictures
          Requiring a version of Perl greater than 5.11
           implicitly turns on use strict
          use 5.12.0;
          Is equivalent to
          use strict;
           use feature ':5.12';



                                                           45
London Perl Workshop
 12th November 2011
Y2038 Compliance
          Core time functions are now Y2038
           compliant




                                               46
London Perl Workshop
 12th November 2011
Smart Match Changes
          Some changes to Smart Match operator
          No longer commutative
          See new table in perlsyn
          Still in flux!




                                                  47
London Perl Workshop
 12th November 2011
New Modules
          Some new modules in the standard
           distribution
          autodie
          parent
             −   Better version of base.




                                              48
London Perl Workshop
 12th November 2011
Perl 5.14
          Released 14 May 2011
             −   5.14.2 26 Sept 2011
          Many new enhancements




                                        49
London Perl Workshop
 12th November 2011
5.14 Enhancements
          Non-destructive substitution
          Container functions accept references
          Package block
          New modules



                                                   50
London Perl Workshop
 12th November 2011
Non-destructive
                        substitution
          New /r option on s/// and tr///
          Copies input
          Acts on copy
          Original unmodifed
          $_ = 'cat';
           $new = s/cat/dog/r'; # $_ remains 'cat'


                                                     51
London Perl Workshop
 12th November 2011
Container functions
                 accept references
          Array & hash functions used to require
           arrays or hashes
            −   push @array, $value
            −   @keys = keys %hash
          Even if you have a reference
            −   push @$arrayref, $value
            −   @keys = keys %$hashref

                                                    52
London Perl Workshop
 12th November 2011
Container functions
                 accept references
          Array & hash functions now accept
           references
            −   push $array_ref, $value
            −   @keys = keys $hash_ref
          Currently experimental


                                               53
London Perl Workshop
 12th November 2011
Package block
         Attach a code block to a package declaration
         package MyPackage { ... }
         Equivalent to
         { package MyPackage; ... }
         Can also declare a version
         package MyPackage 1.23 { ... }


                                                         54
London Perl Workshop
 12th November 2011
New Modules
         Many modules for parsing META files
         CPAN::Meta::YAML & JSON::PP
         CPAN::Meta
         CPAN::Meta::Spec & CPAN::Meta::History
         Module::Metadata


                                                   55
London Perl Workshop
 12th November 2011
New Modules
         Other new modules
         HTTP::Tiny
         Perl::OSType
         Version::Requirements




                                     56
London Perl Workshop
 12th November 2011
Perl 5.16
          Due in spring 2012
          Currently in development at 5.15
             −   5.15.3 – 2011 Sep 21
             −   Code freeze – 2011 Dec 20




                                              57
London Perl Workshop
 12th November 2011
Perl 5.16
          Look for changes in perldelta
             −   perl5150delta
             −   perl5151delta
             −   perl5152delta
             −   perl5153delta




                                           58
London Perl Workshop
 12th November 2011
Some Highlights
          CORE on all keywords
          Continue outside switch
          Breakpoints with filenames
          Remove Perl 4 *.pl




                                         59
London Perl Workshop
 12th November 2011
More Information
          perldoc perl5100delta
          perldoc perl5120delta
          perldoc perl5140delta




                                          60
London Perl Workshop
 12th November 2011
That's all folks
• Any questions?

Weitere ähnliche Inhalte

Mehr von Dave Cross

Measuring the Quality of Your Perl Code
Measuring the Quality of Your Perl CodeMeasuring the Quality of Your Perl Code
Measuring the Quality of Your Perl CodeDave Cross
 
Apollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter BotApollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter BotDave Cross
 
Monoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver BulletsMonoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver BulletsDave Cross
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional ProgrammerDave Cross
 
I'm A Republic (Honest!)
I'm A Republic (Honest!)I'm A Republic (Honest!)
I'm A Republic (Honest!)Dave Cross
 
Web Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your GooglejuiceWeb Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your GooglejuiceDave Cross
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with DancerDave Cross
 
Freeing Tower Bridge
Freeing Tower BridgeFreeing Tower Bridge
Freeing Tower BridgeDave Cross
 
Modern Perl Catch-Up
Modern Perl Catch-UpModern Perl Catch-Up
Modern Perl Catch-UpDave Cross
 
Error(s) Free Programming
Error(s) Free ProgrammingError(s) Free Programming
Error(s) Free ProgrammingDave Cross
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev AssistantDave Cross
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven PublishingDave Cross
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven PublishingDave Cross
 
Perl in the Internet of Things
Perl in the Internet of ThingsPerl in the Internet of Things
Perl in the Internet of ThingsDave Cross
 
Return to the Kingdom of the Blind
Return to the Kingdom of the BlindReturn to the Kingdom of the Blind
Return to the Kingdom of the BlindDave Cross
 
Github, Travis-CI and Perl
Github, Travis-CI and PerlGithub, Travis-CI and Perl
Github, Travis-CI and PerlDave Cross
 
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseDave Cross
 

Mehr von Dave Cross (20)

Measuring the Quality of Your Perl Code
Measuring the Quality of Your Perl CodeMeasuring the Quality of Your Perl Code
Measuring the Quality of Your Perl Code
 
Apollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter BotApollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter Bot
 
Monoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver BulletsMonoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver Bullets
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
I'm A Republic (Honest!)
I'm A Republic (Honest!)I'm A Republic (Honest!)
I'm A Republic (Honest!)
 
Web Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your GooglejuiceWeb Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your Googlejuice
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
 
Freeing Tower Bridge
Freeing Tower BridgeFreeing Tower Bridge
Freeing Tower Bridge
 
Modern Perl Catch-Up
Modern Perl Catch-UpModern Perl Catch-Up
Modern Perl Catch-Up
 
Error(s) Free Programming
Error(s) Free ProgrammingError(s) Free Programming
Error(s) Free Programming
 
Medium Perl
Medium PerlMedium Perl
Medium Perl
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven Publishing
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven Publishing
 
TwittElection
TwittElectionTwittElection
TwittElection
 
Perl in the Internet of Things
Perl in the Internet of ThingsPerl in the Internet of Things
Perl in the Internet of Things
 
Return to the Kingdom of the Blind
Return to the Kingdom of the BlindReturn to the Kingdom of the Blind
Return to the Kingdom of the Blind
 
Github, Travis-CI and Perl
Github, Travis-CI and PerlGithub, Travis-CI and Perl
Github, Travis-CI and Perl
 
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
 

Kürzlich hochgeladen

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Kürzlich hochgeladen (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Modern Core Perl

  • 1. Modern Core Perl Dave Cross Magnum Solutions Ltd dave@mag-sol.com
  • 2. What We Will Cover  Recent Perl releases  5.10  5.12  5.14  5.16 2 London Perl Workshop 12th November 2011
  • 3. Perl Releases  Perl 5 has moved to a regular release cycle  Major release every year − In Spring  Minor releases when required 3 London Perl Workshop 12th November 2011
  • 4. Perl Version Numbers  Even major numbers are production releases − 5.10, 5.12, 5.14  Odd major numbers are dev releases − 5.9, 5.11, 5.13 4 London Perl Workshop 12th November 2011
  • 5. Perl Support  p5p provide support for current and previous major releases − Currently 5.12 and 5.14  Further support may be available from distributors 5 London Perl Workshop 12th November 2011
  • 6. Recent Perl Releases  5.10.0 – 2007 Dec 18  5.10.1 – 2009 Aug 22  5.12.0 – 2010 Apr 12  5.12.1 – 2010 May 16  5.12.2 – 2010 Sep 6  5.12.3 – 2011 Jan 21 6 London Perl Workshop 12th November 2011
  • 7. Recent Perl Releases  5.14.0 – 2011 May 14  5.14.1 – 2011 Jun 16  5.12.4 – 2011 Jun 20  5.14.2 – 2011 Sep 26 7 London Perl Workshop 12th November 2011
  • 8. Perl 5.10  Released 18th Dec 2007 − Perl's 20th birthday  Many new features  Well worth upgrading 8 London Perl Workshop 12th November 2011
  • 9. New Features  Defined-or operator  Switch operator  Smart matching  say()  Lexical $_ 9 London Perl Workshop 12th November 2011
  • 10. New Features  State variables  Stacked file tests  Regex improvements  Many more 10 London Perl Workshop 12th November 2011
  • 11. Defined Or  Boolean expressions “short-circuit”  $val = $val || $default;  $val ||= $default;  What if 0 is a valid value? 11 London Perl Workshop 12th November 2011
  • 12. Defined Or  Need to check “definedness”  $val = defined $val ? $val : $default;  $val = $default unless defined $val; 12 London Perl Workshop 12th November 2011
  • 13. Defined Or  The defined or operator makes this easier  $val = $val // $default;  A different slant on truth  Checks definedness  Shortcircuit version too  $val //= $value; 13 London Perl Workshop 12th November 2011
  • 14. Switch Statement  Switch.pm was added with Perl 5.8  Source filter  Parser limitations − Regular expressions − eval  5.10 introduces a build-in switch statement 14 London Perl Workshop 12th November 2011
  • 15. Given ... When  Switch is spelled “given”  Case is spelled “when”  Powerful matching syntax 15 London Perl Workshop 12th November 2011
  • 16. Given Example  given ($foo) { when (/^abc/) { $abc = 1; } when (/^def/) { $def = 1; } when (/^xyz/) { $xyz = 1; } default { $nothing = 1; } } 16 London Perl Workshop 12th November 2011
  • 17. New Keywords  Four new keywords − given − when − default − continue 17 London Perl Workshop 12th November 2011
  • 18. given  given(EXPR)  Assigns the result of EXPR to $_ within the following block  Similar to do { my $_ = EXPR; ... } 18 London Perl Workshop 12th November 2011
  • 19. when  when (EXPR)  Uses smart matching to compare $_ with EXPR  Equivalent to when ($_ ~~ EXPR)  ~~ is the new smart match operator  Compares two values and “does the right thing” 19 London Perl Workshop 12th November 2011
  • 20. default  default defines a block that is executed if no when blocks match  default block is optional 20 London Perl Workshop 12th November 2011
  • 21. continue  continue keyword falls through to the next when block  Normal behaviour is to break out of given block once the first when condition is matched  Inverse of most other programming languages 21 London Perl Workshop 12th November 2011
  • 22. continue  given($foo) { when (/x/) { say '$foo contains an x'; continue } when (/y/) { say '$foo contains a y' } default { say '$foo contains no x or y' } } 22 London Perl Workshop 12th November 2011
  • 23. Smart Matching  ~~ is the new Smart Match operator  Different kinds of matches  Dependent on the types of the operands  See “perldoc perlsyn” for the full details  Warning: Still under discussion 23 London Perl Workshop 12th November 2011
  • 24. Smart Match Examples  $foo ~~ $bar; # == or cmp  @foo ~~ $bar; # array contains value  %foo ~~ $bar; # hash key exists  $foo ~~ qr{$bar}; # regex match  @foo ~~ @bar; # arrays are identical  %foo ~~ %bar; # hash keys match  Many more alternatives 24 London Perl Workshop 12th November 2011
  • 25. say()  say() is a new alternative to print()  Adds a new line at the end of each call  say($foo); # print $foo, “n”;  Two characters shorter than print  Less typing 25 London Perl Workshop 12th November 2011
  • 26. Lexical $_  $_ is a package variable  Always exists in main package  Can lead to subtle bugs when not localised correctly  Can now use my $_ to create a lexically scoped variable called $_ 26 London Perl Workshop 12th November 2011
  • 27. State Variables  Lexical variables disappear when their scope is destroyed  sub variables { my $x; say ++$x; } variables() for 1 .. 3; 27 London Perl Workshop 12th November 2011
  • 28. State Variables  State variables retain their value when their scope is destroyed  sub variables { state $x; say ++$x; } variables() for 1 .. 3; 28 London Perl Workshop 12th November 2011
  • 29. State Variables  Like static variables in C  Deprecating bugs − my $x if 0; 29 London Perl Workshop 12th November 2011
  • 30. Stacked File Tests  People often think you can do this  -f -w -x $file  Previously you couldn't  Now you can  Equivalent to  -x $file && -w _ && -f _ 30 London Perl Workshop 12th November 2011
  • 31. Regex Improvements  Plenty of regular expression improvements  Named capture buffers  Possessive quantifiers  Relative backreferences  New escape sequences  Many more 31 London Perl Workshop 12th November 2011
  • 32. Named Capture Buffers  Variables $1, $2, etc change if the regex is altered  Named captures retain their names  (?<name> ... ) to define  Use new %+ hash to access them 32 London Perl Workshop 12th November 2011
  • 33. Named Capture Example  while (<DATA>) { if (/(?<header>[ws]+) :s+(?<value>.+)/x) { print "$+{header} -> "; print "$+{value}n"; } } 33 London Perl Workshop 12th November 2011
  • 34. Possessive Quantifiers  ?+, *+, ++  Grab as much as they can  Never give it back  Finer control over backtracking  'aaaa' =~ /a++a/  Never matches 34 London Perl Workshop 12th November 2011
  • 35. Relative Backreferences  g{N}  More powerful version of 1, 2, etc  g{1} is the same as 1  g{-1} is the last capture buffer  g{-2} is the one before that 35 London Perl Workshop 12th November 2011
  • 36. New Escape Sequences  h – Horizontal white space  v – Vertical white space  Also H and V 36 London Perl Workshop 12th November 2011
  • 37. Accessing New Features  Some new features would break backwards compatibility  They are therefore turned off by default  Various ways to turn them on 37 London Perl Workshop 12th November 2011
  • 38. Feature Pragma  Turn new features on with the feature pragma  use feature 'say';  use feature 'switch';  use feature 'state';  use feature ':5.10'; 38 London Perl Workshop 12th November 2011
  • 39. Implicit Loading  Two ways to automatically turn on 5.10 features  Require a high enough version of Perl  use 5.10.0; # Or higher  -E command line option  perl -e 'say “hello”'  perl -E 'say “hello”' 39 London Perl Workshop 12th November 2011
  • 40. Perl 5.12  Released 12 April 2010 − 5.12.4 20 June 2011  Many new enhancements 40 London Perl Workshop 12th November 2011
  • 41. 5.12 Enhancements  package NAME VERSION syntax  ... operator  Implicit strictures  Y2038 compliance 41 London Perl Workshop 12th November 2011
  • 42. 5.12 Enhancements  Smart match changes  New modules − autodie − parent 42 London Perl Workshop 12th November 2011
  • 43. package NAME VER  Declare the version of a package in the package declaration  package My::Package 1.23;  Equivalent to  package My::Package; our $VERSION = 1.23; 43 London Perl Workshop 12th November 2011
  • 44. ... Operator  Called the “yada-yada” operator  Used to stand in for unwritten code  sub unimplemented { ... }  Code compiles  Throws an “unimplemented” exception when run 44 London Perl Workshop 12th November 2011
  • 45. Implicit Strictures  Requiring a version of Perl greater than 5.11 implicitly turns on use strict  use 5.12.0;  Is equivalent to  use strict; use feature ':5.12'; 45 London Perl Workshop 12th November 2011
  • 46. Y2038 Compliance  Core time functions are now Y2038 compliant 46 London Perl Workshop 12th November 2011
  • 47. Smart Match Changes  Some changes to Smart Match operator  No longer commutative  See new table in perlsyn  Still in flux! 47 London Perl Workshop 12th November 2011
  • 48. New Modules  Some new modules in the standard distribution  autodie  parent − Better version of base. 48 London Perl Workshop 12th November 2011
  • 49. Perl 5.14  Released 14 May 2011 − 5.14.2 26 Sept 2011  Many new enhancements 49 London Perl Workshop 12th November 2011
  • 50. 5.14 Enhancements  Non-destructive substitution  Container functions accept references  Package block  New modules 50 London Perl Workshop 12th November 2011
  • 51. Non-destructive substitution  New /r option on s/// and tr///  Copies input  Acts on copy  Original unmodifed  $_ = 'cat'; $new = s/cat/dog/r'; # $_ remains 'cat' 51 London Perl Workshop 12th November 2011
  • 52. Container functions accept references  Array & hash functions used to require arrays or hashes − push @array, $value − @keys = keys %hash  Even if you have a reference − push @$arrayref, $value − @keys = keys %$hashref 52 London Perl Workshop 12th November 2011
  • 53. Container functions accept references  Array & hash functions now accept references − push $array_ref, $value − @keys = keys $hash_ref  Currently experimental 53 London Perl Workshop 12th November 2011
  • 54. Package block  Attach a code block to a package declaration  package MyPackage { ... }  Equivalent to  { package MyPackage; ... }  Can also declare a version  package MyPackage 1.23 { ... } 54 London Perl Workshop 12th November 2011
  • 55. New Modules  Many modules for parsing META files  CPAN::Meta::YAML & JSON::PP  CPAN::Meta  CPAN::Meta::Spec & CPAN::Meta::History  Module::Metadata 55 London Perl Workshop 12th November 2011
  • 56. New Modules  Other new modules  HTTP::Tiny  Perl::OSType  Version::Requirements 56 London Perl Workshop 12th November 2011
  • 57. Perl 5.16  Due in spring 2012  Currently in development at 5.15 − 5.15.3 – 2011 Sep 21 − Code freeze – 2011 Dec 20 57 London Perl Workshop 12th November 2011
  • 58. Perl 5.16  Look for changes in perldelta − perl5150delta − perl5151delta − perl5152delta − perl5153delta 58 London Perl Workshop 12th November 2011
  • 59. Some Highlights  CORE on all keywords  Continue outside switch  Breakpoints with filenames  Remove Perl 4 *.pl 59 London Perl Workshop 12th November 2011
  • 60. More Information  perldoc perl5100delta  perldoc perl5120delta  perldoc perl5140delta 60 London Perl Workshop 12th November 2011
  • 61. That's all folks • Any questions?