London Perl Mongers Teach-In

Dave Cross <dave@mag-sol.com>

Magnum Solutions Ltd

Session 4

  • Various cool stuff

    • Config Files

    • Hack Day Perl

    • Where to go now

Config Files

  • Can you just add...?

  • Users are never satisfied

  • Always want something changed

  • Most users won't edit code

  • Most users can be trained to edit config files

  • Move as much as possible into config files

Configuration In Code

  • This is bad

  •   #!/usr/bin/perl
      use strict;
      use warnings;
      ## Configuration
      my $debug = 0;
      my $email = 'dave@dave.org.uk';
      my $output_file = 'output.txt';
      ## DO NOT EDIT BEYOND THIS POINT

Why Is This Bad?

  • Don't trust 'em

  • Something will get broken

  • Code doesn't match what is in source code control

  • Recipe for disaster

  • Don't do it

Windows INI Files

  •   [debugging]
      debug=1
      verbose=0
  • Most users will understand this

  • Provide them with a dry-run option

    • Tell them what would happen

  • Parse with Config::IniFiles

XML

  •   <debugging>
        <debug>1</debug>
        <verbose>0</debug>
      </debugging>
  • or

      <debugging
         debug="1"
         verbose="0" />
  • Too complicated

  • Parse with XML::Simple (or XML::LibXML / XML::XPath)

YAML

  •   ---
      debugging:
        debug: 1
        verbose: 0
  • Looks a bit like INI file

  • Human-readable

  • Still give them a dry-run option

  • Parse with YAML

The Swiss Army Config Parser

  • Config::Auto

  • "Magical config file parser"

  • Guesses where your config file is

  • Guesses what format it is in

  • And usually gets it right

Other Places for Config Data

  • Environment variables

  • Command line options

    • Parse with Getopt::Long

  • Need to establish an order of precedence

  •   $debug = defined $opt{debug}  ? $opt{debug} :
               defined $ENV{DEBUG}  ? $ENV{DEBUG} :
               defined $conf{debug} ? $conf{debug} :
               0;

Hack Day Perl

  • BBC Backstage / Yahoo! Hack Day

  • June 16th 2007

  • Alexandra Palace

  • Mashups

  • Data Feeds / APIs

  • Web 2.0

  • CPAN modules can help

Reading Web Feeds

  • A lot of data available as web feeds

  • RSS / Atom

  • But which version of RSS

  • RDF vs RSS

Parsing RSS in Perl

  • XML::RSS

  •   use XML::RSS;
      my $rss = XML::RSS->new;
      $rss->parsefile('myfeed.rss');
      foreach (@{$rss->{items}}) {
        print $_->{title}, "\n";
        print $_->{link}, "n";
      }
  • Supports RSS 0.9, 0.91, 1.0 and 2.0

  • Most of the versions you'll see in the wild

Parsing Atom in Perl

  • XML::Atom

  •   use XML::Atom::Feed;
      my $atom = XML::Atom::Feed->new('myatom.xml');
      foreach ($atom->entries) {
        print $_->title, "\n";
        print $_->link, "\n";
      }
  • Cleverer than XML::RSS

  • Handles things like autodiscovery

  •   my @feeds =
        XML::Atom::Feed->find_feeds('http://example.com/');

Broken Feeds

  • Not all XML feeds are valid XML

  • This is bad!

  • You should complain bitterly to the provider

  • But until it is fixed you can use XML::Liberal

    • Slightly badly named

    • What it parses isn't XML

Mashing Up Web Feeds

  • Plagger - Pluggable RSS/Atom Aggregator

  • Huge distribution, installs half of CPAN

  • Dozens of small plugins

  • Single program (plagger)

  • YAML configuration file

Planet Teach-In

Web Services APIs

  • Many web sites expose web services

  • REST, RPC, SOAP

  • HTTP request

  • Parse response

    • Usually XML

Web Services on CPAN

  • Many CPAN modules for handling web services

  • Wrappers around LWP or WWW::Mechanize

  • Parse response into data structures or objects

  • Make dealing with web services very easy

CPAN Examples

  • Net::Backpack

  • Net::Amazon::*

  • Flickr::API

  • Geo::Google

  • Geo::Coder::Yahoo

API Keys

  • Many of these sites will monitor the requests you make

  • Use an API key

  • Apply for the key on the web site

  • Use the key in every request you use

Flickr API

Flickr API Example

  #!/usr/bin/perl
  use strict;
  use warnings;
  use Flickr::API;
  my $flickr = Flickr::API->new({
    key => 'your key goes here'
  });
  my $response = $flickr->execute_method(
    'flickr.photos.getRecent', { per_page => 2 }
  );
  print $response->content;                                  

Flickr API Example Results

  <?xml version="1.0" encoding="utf-8" ?>
  <rsp stat="ok">
  <photos page="1" pages="500" perpage="2" total="1000">
    <photo id="525440809" owner="65666164@N00"
           secret="3ab099b893" server="223" farm="1"
           title="00760052" ispublic="1" isfriend="0"
           isfamily="0" />
    <photo id="525440801" owner="47391132@N00"
           secret="d24451edc8" server="1009" farm="2"
           title="041070 14 29B" ispublic="1" isfriend="0"
           isfamily="0" />
  </photos>
  </rsp>

APIs Without CPAN Modules

  • You might find an API that doesn't have a CPAN module

  • Easy to do it yourself

  • Read the API documentation

  • LWP / WWW::Mechanize

  • Parse the response

  • (Release to CPAN)

Web 2.0 Goodness

  • You don't need to be using Ruby on Rails to use AJAX

  • AJAX is largely just Javascript

  • With a server-side application

  • Which can be written in Perl

AJAX on CPAN

  • CGI::Ajax

    • Creates AJAX code in your CGI program output

    • Similar to Ruby on Rails AJAX helpers

  • JSON

    • Perl module for creating JSON

    • Common format for AJAX data exchange

    • Serialised Javascript Objects

    • A lot like YAML

Where To Now?

  • Some suggestions for places to go for further information

  • Web sites

  • Books

  • Magazines

  • Mailing lists

  • Conferences

London Perl Mongers

  • http://london.pm.org/

  • One of the largest Perl Monger groups

  • Mailing lists

  • Monthly social meetings

  • Occasional technical meetings

  • Annual London Perl Workshop

Web sites

  • use Perl;

    • Perl news site

    • Also journals

  • perl.com

    • O'Reilly run site

    • High quality articles

More Web Sites

  • Perl Monks

    • Best web site for Perl questions

    • Many Perl experts

  • The Perl directory

Books

  • Some recent Perl books

  • Perl Best Practices - Damian Conway

  • Advanced Perl Programming - Simon Cozens

  • Perl Hacks - chromatic, Conway & Poe

  • Intermediate Perl - Schwartz, foy & Phoenix

  • Mastering Perl - brian d foy

More Books

Magazines

Mailing Lists

Conferences

  • The Open Source Convention

    • Portland 23-27 July 2007

  • YAPC

    • Houston 25-25 June 2007

    • Vienna 28-30 August 2007

    • Brasil, Asia, Israel, Australia

  • One-Day Perl Workshops

The End

  • That's all folks

  • Hope it was useful

  • Please fill in the evaluation forms

  • See you at London.pm

  • Which pub are we going to?