Modern Perl
Upcoming SlideShare
Loading in...5
×
 

Modern Perl

on

  • 8,737 views

A brief introduction to modern Perl programming tools that I gave at the OpenTech conference in Spetember 2010.

A brief introduction to modern Perl programming tools that I gave at the OpenTech conference in Spetember 2010.

Statistics

Views

Total Views
8,737
Views on SlideShare
8,232
Embed Views
505

Actions

Likes
7
Downloads
123
Comments
0

14 Embeds 505

http://perlhacks.com 268
http://allframeworks.ru 208
http://dave.org.uk 7
http://sayperl.org 5
http://www.allframeworks.ru 4
http://theoldreader.com 3
http://lanyrd.com 2
http://perlsphere.net 2
http://static.slidesharecdn.com 1
http://www.dave.org.uk 1
http://orhowilearnedtolovetheperl.com 1
http://xss.yandex.net 1
http://192.168.178.200:3000 1
http://alf.local 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Modern Perl Modern Perl Presentation Transcript

  • Modern Perl
  • Fifteen years of Perl development
  • In twenty minutes
  • 1995
  • Everyone uses Perl to build dynamic web sites
  • Nasty CGI scripts
  • Web technology has moved on since then
  • Perl technology has moved on since then
  • Perceptions of Perl are stuck in the mid 90s
  • Hi! We're the Perl community and we suck at marketing
  • Perl has all the facilities you would expect in a modern dynamic language
  • Fully! Buzzword! Compliant!
  • A note on version numbers
  • Perl 5 is the current version of Perl
  • Specifically 5.12.1
  • Specifically 5.12.
  • Specifically 5.12.2
  • Perl 6 is still in development
  • (If you want to know more about Perl 6 then ask me later)
  • Perl 5 is still thriving
  • Some powerful Perl tools
  • Template Toolkit
  • Templating engine
  • Powerful and flexible
  • Web and non-web
  • Separation of concerns
  • Dear [% name %], You owe me £[% debt %]. Please pay up by [% date %] or I'll send the boys round. Love Dave...
  • #!/usr/bin/perl use Template; my $tt = Template->new; my $data = { name => 'Joe Random', debt => 100, date => '18 September', }; $tt->process('template.tt', $data);
  • Use with objects
  • [% FOREACH debt IN debts %] Dear [% debt.name %], You owe me £[% debt.amount %]. Please pay up by [% debt.date %] or I'll send the boys round. Love Dave... [% END %]
  • #!/usr/bin/perl use Template; use Debt; my $tt = Template->new; my @debts = Debt->find_all; $tt->process('template.tt', { debts => @debts});
  • http://tt2.org/
  • ORM
  • We all hate SQL
  • DBIx::Class
  • Builds on DBI
  • DBIx::Class::Schema::Loader
  • $ dbicdump MyClass 'dbi:mysql:<db>;<hostname>' <user> <password> Dumping manual schema for MyClass to directory . ... Schema dump completed.
    • Class for each table
    • Attribute for each column
      • Type, mandatory/optional, auto-increment
      • Data type inflation
    • Relationships
  • #!/usr/bin/perl use MyClass; my $sch = MyClass->connect('...'); my $objs = $sch->resultset('MyTable'); while ($objs->next) { print $_->name, “ ”; }
  • while (<FILE>) { my ($code, $name, $desc) = split; my $new_obj = $objs->create({ code => $code, name => $name, desc => $desc, }); print 'New object id: ', $new_obj->id, “ ”; }
    • Complex searching
    • Prefetching data
    • Many-to-many relationships
    • Database migrations
    • Replicated databases
  • http://dbix-class.org/
  • Moose
  • The Modern Object System for Perl 5
  • Perl 5's standard OO system looks a bit bolted on
  • (That's because it was bolted on)
  • Moose hides all that nastiness
  • Pretty syntactic sugar
  • Declarative syntax for attributes
  • package Debt; use Moose; has name => (isa => 'Str', is => 'rw', required => 1); has amount => (isa => 'Num', is => 'rw', required => 1); has date => (isa => 'DateTime', is => 'rw');
  • #!/usr/bin/perl use 5.010; use strict; use warnings; use Debt; my $debt = Debt->new({ name => 'Joe Random', amount => 100, }); say $debt->name, ' owes £', $debt->amount; # Add interest $debt->amount($debt->amount * 1.1); say $debt->name, ' owes £', $debt->amount;
  • use DateTime; # Set due date $debt->date(DateTime->now->add(days => 28)); say $debt->date; # Easier to read say $debt->date->strftime('%A %d %B %Y');
    • More attribute features
      • Lazy build
      • Delegation
      • Defaults
      • Triggers
    • Roles/Traits
      • Like mixins or interfaces
  • MooseX::*
  • MooSex::*
  • MooseX::*
  • http://moose.perl.org/
  • MVC
  • Catalyst
  • Builds on existing tools
  • Model is DBIx::Class
  • View is Template Toolkit
  • (These are just defaults)
  • Easy to get application framework running
  • $ catalyst.pl MyApp created &quot;MyApp&quot; created &quot;MyApp/script&quot; created &quot;MyApp/lib&quot; created &quot;MyApp/root&quot; created &quot;MyApp/root/static&quot; created &quot;MyApp/root/static/images&quot; created &quot;MyApp/t&quot; [ ... ] created &quot;MyApp/Makefile.PL&quot; created &quot;MyApp/script/myapp_cgi.pl&quot; created &quot;MyApp/script/myapp_fastcgi.pl&quot; created &quot;MyApp/script/myapp_server.pl&quot; created &quot;MyApp/script/myapp_test.pl&quot; created &quot;MyApp/script/myapp_create.pl&quot; Change to application directory and Run &quot;perl Makefile.PL&quot; to make sure your install is complete
  • $ cd MyApp $ script/myapp_server.pl [debug] Debug messages enabled [debug] Statistics enabled [debug] Loaded plugins: .----------------------------------------------------------------------------. | Catalyst::Plugin::ConfigLoader 0.27 | '----------------------------------------------------------------------------' [ ... lots of information ... ] [info] MyApp powered by Catalyst 5.80023 You can connect to your server at http://localhost:3000
  •  
  • Plugins to handle common requirements
  • Authentication/Authorisation
  • Session handling
  • CatalystX::*
  • Catalyst::Plugin::AUTOCRUD
  • http://catalystframework.org/
  • PSGI / Plack
  • PSGI is a specification
  • Like a super-charged CGI
  • And a lot like WSGI
  • Plack is a reference implementation
  • And a lot like Rack
  • Make it easy to move web apps
  • Different application technologies
  • Different web hosting technologies
  • Take a PSGI application and run it anywhere
  • PSGI app is a subroutine reference
  • # app.psgi my $app = sub { # clever stuff goes here };
  • Input comes from a hash passed to sub
  • # app.psgi my $app = sub { my $env = shift; # clever stuff goes here };
  • Sub returns a reference to an array
  • # app.psgi my $app = sub { my $env = shift; return [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world' ], ]; };
  • Plack distribution includes many tools
  • plackup - a simple PSGI web server
  • $ plackup app.psgi HTTP::Server::PSGI Accepting connections at http://localhost:5000/
  • Plack::Request Plack::Response
  • use Plack::Request; use Plack::Response; use Data::Dumper; my $app = sub { my $req = Plack::Request->new(shift); my $res = Plack::Response->new(200); $res->content_type('text/plain'); $res->body(Dumper $req); return $res->finalize; }
  • Plack::Middleware::* Plack::App::*
  • Most Perl web frameworks support Plack
  • http://plackperl.org/
  • Other Modern Perl tools
  • DateTime
  • TryCatch & autodie
  • Test::* & TAP::*
  • A lot has happened in the ten years you've been ignoring Perl
  • Your local Perl Mongers will be happy to tell you more
  • http://london.pm.org/ http://pm.org/
  • Dave Cross [email_address] @davorg @perlfoundation
  •