London Perl Mongers Teach-In

Dave Cross <dave@mag-sol.com>

Magnum Solutions Ltd

Introduction

Perl Teach-In

  • Advanced level training for Perl programmers

  • Turn intermediate programmers into advanced programmers

  • Attempt to fill some of the many current Perl vacancies in London

  • Perl is not dying

Perl Recruitment

A Word From Our Sponsors

  • BBC Backstage

  • http://backstage.bbc.co.uk/

  • backstage.bbc.co.uk is the BBC's developer network to encourage innovation and support new talent.

  • Various feeds and APIs

  • Lots of lovely data to mashup

  • Free stuff

Another Word From Our Sponsors

Perl Teach-In

Timetable

Timetable

  • 10:00 Session 1

    • CPAN

    • Templating

  • 11:15 Coffee break

Timetable

  • 11:30 Session 2

    • ORM

    • Testing

    • Benchmarking

  • 12:30 Lunch

Timetable

  • 13:30 Session 3

    • Object Oriented Perl

  • 14:45 Coffee Break

Timetable

  • 15:00 Session 4

    • Various cool stuff

  • 16:00 End (Pub?)

Session 1

  • CPAN

  • Templating

CPAN

CPAN

  • Not going to introduce CPAN to you

  • Additional CPAN services

  • CPANTS

  • CPAN Projects

CPAN Tools

CPAN Tools

Automatic Smoke Testing

  • All modules uploaded to CPAN are automatically smoke tested

  • Downloaded, build and tested by over 400 testers

  • Multiple platforms

  • See the results at http://cpantesters.perl.org/

CPAN Testers

CPAN Tools

CPAN Tools

CPAN::Forum

CPAN Tools

CPAN Tools

CPAN Request Tracker

CPAN Tools

CPAN Tools

CPAN Tools

CPAN Tools

CPAN Tools

CPAN Ratings

CPAN Ratings

CPAN Tools

CPANTS

Kwalitee

  • It looks like quality

  • It sounds like quality

  • But it's not quite quality

  • Number of automatically measurable criteria to rate CPAN modules

Sample Kwalitee Measures

  • has_readme, has_manifest, has_metayml, has_buildtool

  • has_tests

  • has_version, has_proper_version

  • no_pod_errors

  • has_test_pod, has_test_pod_coverage

  • use_strict

  • is_prereq

CPANTS

The CPANTS Game

  • No kwalitee criterion is a quarantee of quality

  • The just tend to be seen in the same place as quality

  • It's possible to "game" the system

  • Produce modules that are written to raise kwalitee

    • Acme::Raise_my_kwalitee

  • Ensure kwalitee is high before releasing module

    • Test::Kwalitee

The CPANTS Game

CPAN Projects

  • CPAN has very low entry requirements

  • Not very hard to get a CPAN login

  • This is generally a good thing

  • Many modules doing the same thing

    • All of them subtly different

    • None of them doing exactly what you want

Dates and Times

  • Dozens of date/time modules on CPAN

  • Date::Manip is almost never what you want

  • Date::Calc, Date::Parse

  • Class::Date

  • Time::Piece

  • Date::Simple

  • What do you choose?

DateTime Project

  • http://datetime.perl.org/

  • "The DateTime family of modules present a unified way to handle dates and times in Perl"

  • "... there are several different packages with overlapping functionality, but it was hard to take dates in one package and convert them to another which you would often have to do to get a function from a different package."

  • "unified" is good

  • Dozens of modules that work together in a consistant fashion

Using DateTime

  • Simple case

      #!/usr/bin/perl
      use DateTime;
      my $dt = DateTime->now;
      print $dt;            # 2007-05-26T16:06:07
      print $dt->dmy, "\n"; # 2007-05-26
      print $dt->hms, "\n"; # 16:06:07

Using DateTime

  • More advanced constructor

      #!/usr/bin/perl
      use DateTime;
      my $dt = DateTime->new(year  => 2007,
                             month => 6,
                             day   => 2);
      print $dt->ymd('/'), "\n";   # 2007/06/02
      print $dt->month, "\n";      # 6
      print $dt->month_name, "\n"; # June

Using DateTime

  • A DateTime object is a point in time

  • For date arithmatic you need a duration

      #!/usr/bin/perl
      use DateTime;
      my $dt = DateTime->new(year => 2007, month => 6,
                             day => 2);
      my $two_weeks = DateTime::Duration->new(weeks => 2);
      $dt += $two_weeks;
      print $dt, "\n"; # 2007-06-16T00:00:00

Using Datetime

  • Output formats with strftime

      #!/usr/bin/perl
      use DateTime;
      my $dt = DateTime->new(year => 2007, month => 6,
                             day => 2);
      print $dt->strftime('%A, %d %B %Y'), "\n";
      # Saturday, 02 June 2007
  • Input format with DateTime::Format::Strptime

Parsing/formatting dates

  • DateTime::Format::HTTP

  • DateTime::Format::MySQL

  • DateTime::Format::Excel

  • DateTime::Format::Baby (the big hand is on...)

Alternative calendars

  • DateTime::Calendar::Julian

  • DateTime::Calendar::Hebrew

  • DateTime::Calendar::Mayan

  • DateTime::Fiction::JRRTolkien::Shire

Perl Email Project

  • Similar problem as with dates

  • Dozens of overlapping modules

  • Don't interact well

  • The Perl Email Project (PEP) is a community dedicated to making Perl the best choice language for email processing. It maintains existing code, documents known problems and solutions, and develops new tools.

  • http://emailproject.perl.org/

Templating

Templating

  • Most people use templates to produce web pages

  • Advantages are well known

  • Standard look and feel (static/dynamic)

  • Reusable components

  • Separation of code logic from display logic

  • Different skill-sets (HTML vs Perl)

Non-Web Templating

  • The same advantages apply to non-web areas

  • Reports

  • Business documents

  • Configuration files

  • Anywhere you produce output

Writing a Templating System

  • Must be easy - so many people do it

  • See perlfaq4

  • How can I expand variables in text strings?

  •   $text = 'this has a $foo in it and a $bar';
      %user_defs = (
        foo  => 23,
        bar  => 19,
      );
      $text =~ s/\$(\w+)/$user_defs{$1}/g;
  • Don't do that

Perl Templating Options

  • Dozens of template modules on CPAN

  • Text::Template, HTML::Template, Mason, Template Toolkit

  • Many, many more

  • Questions to consider

    • HTML only?

    • Template language

  • I choose the Template Toolkit

Template Tookit

  • http://tt2.org/

  • Very powerful

  • Both web and non-web

  • Simple template language

  • Plugins give access to much of CPAN

  • Can use Perl code if you want

    • But don't do that

Good Book Too

The Template Equation

  • Data + Template = Output

  • Data + Alternative Template = Alternative Output

  • Different views of the same data

  • Only the template changes

Simple Template Application

  #!/usr/bin/perl
  use Template;
  use My::Object;
  my ($id, $format) = @ARGV;
  $format ||= 'html';
  my $obj = My::Object->new($id) or die;
  my $tt  = Template->new;
  $tt->process("$format.tt", { obj => $obj },
               "$id.$format")
    or die $tt->error;

Adding New Formats

  • No new code required

  • Just add new output template

  • Perl programmer need not be involved

The Template Equation Revisited

  • Data + Template = Output

    • Template Toolkit

  • Template + Output = Data

    • Template::Extract

  • Data + Output = Template

    • Template::Generate

Coffee Break

  • Back in 15 minutes