Perl
Perl
Hate it for the right reasons
“Perl is an abomination.”
“Perl is an abomination.”
               Matt Follett, circa 2006
“Perl is an abomination.”
                               Matt Follett, circa 2006
     while talking to president of the Perl Foundation
Old Views of Perl
$;=$/;seek+DATA,!++$/,!$s;$_=<DATA>;$s&&print||$g&&do{$y=($x||=20)*($y||8);sub
i{sleep&f}sub'p{print$;x$=,join$;,$b=~/.{$x}/g}$j=$j;sub'f{pop}sub
n{substr($b,&f%$y,3)=~tr,O,O,}sub'g{$f=&f-1;($w,$w,substr($b,&f,1),O)[n($f-$x)+
n($x+$f)-(substr($b,&f,1)eq+O)+n$f]||$w}$w="40";$b=join'',@ARGV?<>:$_,$w
x$y;$b=~s).)$&=~/w/?O:$w)ge;substr($b,$y)=q++;$g='$i=0;$i?$b:$c=$b;
substr+$c,$i,1,g$i;$g=~s?d+?($&+1)%$y?e;$i-$y+1?eval$g:do{$i=-1;$b=$c;p;i
1}';sub'e{eval$g;&e}e}||eval||die+No.$;
__DATA__
if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}}
@s=(q[$_=sprintf+pop@s,@s],q[
if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}} #_The_Perl_Journal_#
@s=(q[%s],q[%s])x2;%s;print"n"x&_,$_;i$j;eval}
])x2;$_=sprintf+pop@s,@s;print"n"x&_,$_;i$j;eval}$/=$y;$"=",";print
q<#!/usr/local/bin/perl -sw
if(!$s){>.($_=<>).q<}else{@s=(q[printf+pop@s,@s],q[#!/usr/local/bin/perl -sw
if(!$s){>.(s$%$%%$g,tr=[=[===tr=]=]=||&d,$_).q<}else{@s=(q[%s],q[%s])x2;%s}
])x2;printf+pop@s,@s}
>
                                            SelfGOL - obfuscated Perl contest entry by Damien Conway
Slightly More Modern Perl
package PerkyProfiler::Service::Role::OwnsUri;
use Moose::Role;
requires 'source';
use URI;

sub owns_uri {
  my ($self, $uri) = @_;

     $uri = URI->new($uri) if not UNIVERSAL::isa( $uri, 'URI' );
     my $source = $self->source;
     return ( $uri->host() =~ /.$source./i )
}
1;
Modern Perl
use MooseX::Declare; use MooseX::MultiMethod;
use Moose::Role;
use URI;

role PerkyProfiler::Service::Role::OwnsUri requires 'source'
{
  multi method owns_uri( URI $uri) {

      my $source = $self->source;

      return ( $uri->host() =~ /.$source./i )
  }
}
1;
Hate it because it does a lot
Meta Object Protocol
new object system on top of MOP
Several nice web frameworks
functional programming
Hate it because it’s
hard to give a talk on
Project
Vending machine
Project
Vending machine
   except, instead of vending items it vends ‘identities’
Project
Vending machine
   except, instead of vending items it vends ‘identities’
   and instead of taking ‘coins’ it takes ‘URL’s
Project
Vending machine
   except, instead of vending items it vends ‘identities’
   and instead of taking ‘coins’ it takes ‘URL’s
   and instead of being a ‘machine’ it is a ‘website’
Project
Vending machine
   except, instead of vending items it vends ‘identities’
   and instead of taking ‘coins’ it takes ‘URL’s
   and instead of being a ‘machine’ it is a ‘website’
   and instead of having states it is ‘stateless’
Project
Vending machine
   except, instead of vending items it vends ‘identities’
   and instead of taking ‘coins’ it takes ‘URL’s
   and instead of being a ‘machine’ it is a ‘website’
   and instead of having states it is ‘stateless’
   ...
PerkyProfiler
The post modern multi service
profiling application all the kids
 are talking about these days.
Goals of project
Given a URL
    get info about the contributor on that service
    comb other services for information on that user
Services chosen
    GitHub
    Flickr
    Twitter
Use CPAN when possible
Web Frameworks

Catalyst
Gantry
Jifty
CGI::App
Catalyst
Simple
MVC Framework
Has many Models and Views to choose from
Easy to write new ones
Easy to plug other things in
Lots of helper scripts
Helpers
Start an app:
   catalyst.pl PerkyProfiler
Add a model:
   ./script/perkyprofiler_create.pl model Profiler
Add a view:
   ./script/perkyprofiler_create.pl view Main TTSite
Incorporating a new Model
package PerkyProfiler::Model::Profiler;

use strict;
use warnings;
use parent 'Catalyst::Model::Adaptor';

__PACKAGE__->config( class => 'PerkyProfiler::Profiler');
Use new model
sub index :Path :Args(0) {
    my ( $self, $c ) = @_;
...
    my $profiler = $c->model('Profiler');

  my $entity;
  eval {
     $entity = $profiler->generate_entity_from_uri($c->req->param('url'));
     $profiler->loop_update_entity($entity);
     $c->stash->{'entity'} = $entity;
  };
  if( my $err = $@) {
     $c->stash->{'message'} = "Sorry, but an error occured:" . $err->message(); }}
Profiler

loop_update_entity
   given an entity loops through and updates it
generate_entity_from_uri
   Generates a new entity object from a given URI
Example Service: Flickr
package PerkyProfiler::Service::Flickr;

use Flickr::Simple2;
use PerkyProfiler::Entity;
use PerkyProfiler::Entity::Attribute;

use Moose;
with 'PerkyProfiler::Service::Role';

has _core => ( isa => 'Flickr::Simple2', is => 'rw');
has key => ( isa => 'Str', is => 'ro' );
has secret => ( isa => 'Str', is => 'ro' );
has _contributed => ( isa => 'Bool', is => 'rw', default => 0 );
Moose
“Post Modern” Object System that provides:
   roles
   declarative definitions
   delegation
   typing
   method modifiers
   …and more!
Declarative Style Classes Ex
has _core => (
   isa => ‘Flickr::Simple2’,
   is => ‘rw’,
   handles =>
     { entity_from_username => 'get_user_byUsername'
       entity_from_url       => 'get_user_byUrl ' }
);
Declarative Style Class Defs
Simply say what it has, eg, “has key => ( isa => 'Str',   is => 'ro' )”
Declare different things:
    is - read/write permissions
    default - default value, or sub that generates the value
    lazy - lazy generation
    required
    delegated tasks
    isa - type
Types?
Yes, perl is loosely typed, but Moose checks for you.
It can do:
   predefined types like strings, integers, numbers
   ref too, eg, ArrayRef[CodeRef] is an array of subs
   classes
   custom types
Custom Types
New types
   type ‘two_chars’ => where { defined $_ && length
   $_ == 2};
Subtypes
   subtype ‘EvenInt’ => as ‘Int’ => where { $_ % 2 + 1}
   => message {“Please provide an even number”}
Back to the example
package PerkyProfiler::Service::Flickr;

use Flickr::Simple2;
use PerkyProfiler::Entity;
use PerkyProfiler::Entity::Attribute;

use Moose;
with 'PerkyProfiler::Service::Role';

has _core => ( isa => 'Flickr::Simple2', is => 'rw');
has key => ( isa => 'Str', is => 'ro' );
has secret => ( isa => 'Str', is => 'ro' );
has _contributed => ( isa => 'Bool', is => 'rw', default => 0 );
Moose Roles
 Roles can be used simply by saying: “with
 'PerkyProfiler::Service::Role';”

 Roles allow you to define a list of methods that must exist
 (an interface)
 Roles also allow you to define a list of methods that
 cannot exist
 Finally roles can provide you with additional functionality
 (mixin)
New Example: Service::Role
package PerkyProfiler::Service::Role;

use Moose::Role;
with 'PerkyProfiler::Service::Role::UsernameGenerator';
with 'PerkyProfiler::Service::Role::OwnsUri';

requires '_get_user_from_uri';
requires '_get_user_info';
requires 'map_to_entity';
requires 'contributor';
Providing a Role
Just `use Moose::Role;`
Roles can consume other Roles
Roles can require subs via requires, eg, requires
‘map_to_entity’
Roles can exclude other roles via excludes, eg, exclude
‘ConflictingRole’
Example: Working with Roles
        Objects can be introspected for roles

foreach my $service ( $self->_services->gen_iterator->() )
{
   if($service->meta->does_role($owns_uri_role))
{
      if($service->owns_uri($uri))
{
            return $service->entity_from_uri($uri);
}}}
What else can Moose do?

Meta everything (Meta attributes, classes, roles)
Anonymous classes
autoboxing with MooseX::Autobox
multimethod dispatching with MooseX::MultiMethod
What else does Perl have?

DBIx::Class
   Object Relational Mapper
   classes are based off tables, but can be reassigned
   columns can be custom inflated
What we missed
Perl’s Functional
Programming Techniques
St. Louis Perl Monger’s proudly present:

 August 17th, 6:30 PM
4444 Forest Park Pkwy
  Intro to Functional
 Programming in Perl
                                           with Bill Odom

Perl: Hate it for the Right Reasons

  • 1.
  • 2.
    Perl Hate it forthe right reasons
  • 3.
    “Perl is anabomination.”
  • 4.
    “Perl is anabomination.” Matt Follett, circa 2006
  • 5.
    “Perl is anabomination.” Matt Follett, circa 2006 while talking to president of the Perl Foundation
  • 6.
    Old Views ofPerl $;=$/;seek+DATA,!++$/,!$s;$_=<DATA>;$s&&print||$g&&do{$y=($x||=20)*($y||8);sub i{sleep&f}sub'p{print$;x$=,join$;,$b=~/.{$x}/g}$j=$j;sub'f{pop}sub n{substr($b,&f%$y,3)=~tr,O,O,}sub'g{$f=&f-1;($w,$w,substr($b,&f,1),O)[n($f-$x)+ n($x+$f)-(substr($b,&f,1)eq+O)+n$f]||$w}$w="40";$b=join'',@ARGV?<>:$_,$w x$y;$b=~s).)$&=~/w/?O:$w)ge;substr($b,$y)=q++;$g='$i=0;$i?$b:$c=$b; substr+$c,$i,1,g$i;$g=~s?d+?($&+1)%$y?e;$i-$y+1?eval$g:do{$i=-1;$b=$c;p;i 1}';sub'e{eval$g;&e}e}||eval||die+No.$; __DATA__ if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}} @s=(q[$_=sprintf+pop@s,@s],q[ if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}} #_The_Perl_Journal_# @s=(q[%s],q[%s])x2;%s;print"n"x&_,$_;i$j;eval} ])x2;$_=sprintf+pop@s,@s;print"n"x&_,$_;i$j;eval}$/=$y;$"=",";print q<#!/usr/local/bin/perl -sw if(!$s){>.($_=<>).q<}else{@s=(q[printf+pop@s,@s],q[#!/usr/local/bin/perl -sw if(!$s){>.(s$%$%%$g,tr=[=[===tr=]=]=||&d,$_).q<}else{@s=(q[%s],q[%s])x2;%s} ])x2;printf+pop@s,@s} > SelfGOL - obfuscated Perl contest entry by Damien Conway
  • 7.
    Slightly More ModernPerl package PerkyProfiler::Service::Role::OwnsUri; use Moose::Role; requires 'source'; use URI; sub owns_uri { my ($self, $uri) = @_; $uri = URI->new($uri) if not UNIVERSAL::isa( $uri, 'URI' ); my $source = $self->source; return ( $uri->host() =~ /.$source./i ) } 1;
  • 8.
    Modern Perl use MooseX::Declare;use MooseX::MultiMethod; use Moose::Role; use URI; role PerkyProfiler::Service::Role::OwnsUri requires 'source' { multi method owns_uri( URI $uri) { my $source = $self->source; return ( $uri->host() =~ /.$source./i ) } } 1;
  • 9.
    Hate it becauseit does a lot Meta Object Protocol new object system on top of MOP Several nice web frameworks functional programming
  • 10.
    Hate it becauseit’s hard to give a talk on
  • 11.
  • 12.
    Project Vending machine except, instead of vending items it vends ‘identities’
  • 13.
    Project Vending machine except, instead of vending items it vends ‘identities’ and instead of taking ‘coins’ it takes ‘URL’s
  • 14.
    Project Vending machine except, instead of vending items it vends ‘identities’ and instead of taking ‘coins’ it takes ‘URL’s and instead of being a ‘machine’ it is a ‘website’
  • 15.
    Project Vending machine except, instead of vending items it vends ‘identities’ and instead of taking ‘coins’ it takes ‘URL’s and instead of being a ‘machine’ it is a ‘website’ and instead of having states it is ‘stateless’
  • 16.
    Project Vending machine except, instead of vending items it vends ‘identities’ and instead of taking ‘coins’ it takes ‘URL’s and instead of being a ‘machine’ it is a ‘website’ and instead of having states it is ‘stateless’ ...
  • 17.
    PerkyProfiler The post modernmulti service profiling application all the kids are talking about these days.
  • 18.
    Goals of project Givena URL get info about the contributor on that service comb other services for information on that user Services chosen GitHub Flickr Twitter Use CPAN when possible
  • 19.
  • 20.
    Catalyst Simple MVC Framework Has manyModels and Views to choose from Easy to write new ones Easy to plug other things in Lots of helper scripts
  • 21.
    Helpers Start an app: catalyst.pl PerkyProfiler Add a model: ./script/perkyprofiler_create.pl model Profiler Add a view: ./script/perkyprofiler_create.pl view Main TTSite
  • 22.
    Incorporating a newModel package PerkyProfiler::Model::Profiler; use strict; use warnings; use parent 'Catalyst::Model::Adaptor'; __PACKAGE__->config( class => 'PerkyProfiler::Profiler');
  • 23.
    Use new model subindex :Path :Args(0) { my ( $self, $c ) = @_; ... my $profiler = $c->model('Profiler'); my $entity; eval { $entity = $profiler->generate_entity_from_uri($c->req->param('url')); $profiler->loop_update_entity($entity); $c->stash->{'entity'} = $entity; }; if( my $err = $@) { $c->stash->{'message'} = "Sorry, but an error occured:" . $err->message(); }}
  • 24.
    Profiler loop_update_entity given an entity loops through and updates it generate_entity_from_uri Generates a new entity object from a given URI
  • 25.
    Example Service: Flickr packagePerkyProfiler::Service::Flickr; use Flickr::Simple2; use PerkyProfiler::Entity; use PerkyProfiler::Entity::Attribute; use Moose; with 'PerkyProfiler::Service::Role'; has _core => ( isa => 'Flickr::Simple2', is => 'rw'); has key => ( isa => 'Str', is => 'ro' ); has secret => ( isa => 'Str', is => 'ro' ); has _contributed => ( isa => 'Bool', is => 'rw', default => 0 );
  • 26.
    Moose “Post Modern” ObjectSystem that provides: roles declarative definitions delegation typing method modifiers …and more!
  • 27.
    Declarative Style ClassesEx has _core => ( isa => ‘Flickr::Simple2’, is => ‘rw’, handles => { entity_from_username => 'get_user_byUsername' entity_from_url => 'get_user_byUrl ' } );
  • 28.
    Declarative Style ClassDefs Simply say what it has, eg, “has key => ( isa => 'Str', is => 'ro' )” Declare different things: is - read/write permissions default - default value, or sub that generates the value lazy - lazy generation required delegated tasks isa - type
  • 29.
    Types? Yes, perl isloosely typed, but Moose checks for you. It can do: predefined types like strings, integers, numbers ref too, eg, ArrayRef[CodeRef] is an array of subs classes custom types
  • 30.
    Custom Types New types type ‘two_chars’ => where { defined $_ && length $_ == 2}; Subtypes subtype ‘EvenInt’ => as ‘Int’ => where { $_ % 2 + 1} => message {“Please provide an even number”}
  • 31.
    Back to theexample package PerkyProfiler::Service::Flickr; use Flickr::Simple2; use PerkyProfiler::Entity; use PerkyProfiler::Entity::Attribute; use Moose; with 'PerkyProfiler::Service::Role'; has _core => ( isa => 'Flickr::Simple2', is => 'rw'); has key => ( isa => 'Str', is => 'ro' ); has secret => ( isa => 'Str', is => 'ro' ); has _contributed => ( isa => 'Bool', is => 'rw', default => 0 );
  • 32.
    Moose Roles Rolescan be used simply by saying: “with 'PerkyProfiler::Service::Role';” Roles allow you to define a list of methods that must exist (an interface) Roles also allow you to define a list of methods that cannot exist Finally roles can provide you with additional functionality (mixin)
  • 33.
    New Example: Service::Role packagePerkyProfiler::Service::Role; use Moose::Role; with 'PerkyProfiler::Service::Role::UsernameGenerator'; with 'PerkyProfiler::Service::Role::OwnsUri'; requires '_get_user_from_uri'; requires '_get_user_info'; requires 'map_to_entity'; requires 'contributor';
  • 34.
    Providing a Role Just`use Moose::Role;` Roles can consume other Roles Roles can require subs via requires, eg, requires ‘map_to_entity’ Roles can exclude other roles via excludes, eg, exclude ‘ConflictingRole’
  • 35.
    Example: Working withRoles Objects can be introspected for roles foreach my $service ( $self->_services->gen_iterator->() ) { if($service->meta->does_role($owns_uri_role)) { if($service->owns_uri($uri)) { return $service->entity_from_uri($uri); }}}
  • 36.
    What else canMoose do? Meta everything (Meta attributes, classes, roles) Anonymous classes autoboxing with MooseX::Autobox multimethod dispatching with MooseX::MultiMethod
  • 37.
    What else doesPerl have? DBIx::Class Object Relational Mapper classes are based off tables, but can be reassigned columns can be custom inflated
  • 38.
  • 39.
  • 40.
    St. Louis PerlMonger’s proudly present: August 17th, 6:30 PM 4444 Forest Park Pkwy Intro to Functional Programming in Perl with Bill Odom