modern perl
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:;'
Dumping manual schema for MyClass to directory . ...
Schema dump completed.
Class for each table
Attribute for each columnType, 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, \n;
}
while () {
my ($code, $name, $desc) = split;
my $new_obj = $objs->create({
code => $code,
name => $name,
desc => $desc,
});
print 'New object id: ', $new_obj->id, \n;
}
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 featuresLazy build
Delegation
Defaults
Triggers
Roles/TraitsLike 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 "MyApp"
created "MyApp/script"
created "MyApp/lib"
created "MyApp/root"
created "MyApp/root/static"
created "MyApp/root/static/images"
created "MyApp/t"
[ ... ]
created "MyApp/Makefile.PL"
created "MyApp/script/myapp_cgi.pl"
created "MyApp/script/myapp_fastcgi.pl"
created "MyApp/script/myapp_server.pl"
created "MyApp/script/myapp_test.pl"
created "MyApp/script/myapp_create.pl"
Change to application directory and Run "perl Makefile.PL" 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 protected]
@davorg
@perlfoundation