Technology for NLP


NPFL092

Zdeněk Žabokrtský & Rudolf Rosa

{zabokrtsky,rosa}@ufal.mff.cuni.cz

Tuesday 9.00–11.20
SU2

Today

1. CPAN

2. Object Oriented Perl

Introduction to CPAN

The Comprehensive Perl Archive Network

CPAN Distribution

Installing Modules from CPAN

Common Prerequisite

A place where you can install to.

Installing Modules by cpan

Installing Modules by cpan (cont.)

Two ways how to use cpan:

  1. Module name as a command line argument:
    $ cpan -i Module::Starter
  2. From the interactive shell:
    $ cpan
    cpan[1]> install Module::Starter

Installing Modules Manually

  1. Download and unpack
    $ tar xzf Module.tgz
  2. Distributions based on ExtUtils::MakeMaker
    perl Makefile.PL
    make
    make test
    make install
  3. Distributions based on Module::Build
    perl Build.PL
    ./Build
    ./Build test
    ./Build install

Object Oriented Programming

Fundamental concepts

Class
a template for objects, describing their characteristics (attributes) and functionality (methods)
Instance
the actual object created at run-time
Method
a set of procedural statements manipulating with the object

Objects in Perl

There are Many Ways…

“Raw” Perl 5 objects (see later)
  • Perl 5 is not a modern OO language
  • objects: blessed hashes, attributes: keys
  • various extensions automating construction, destruction, inheritance, encapsulation, etc.
Inside-out classes (see Perl Best Practices)
  • Class::Std by Damian Conway
  • better encapsulation → more robust code
  • attributes: hashes, objects: keys (cf. above)
Moose
  • still just an extension of Perl 5
  • more modern (inspired by Perl 6)
  • more consistent
  • less tedious

“Raw” Perl 5 Objects

“Raw” Perl 5 Example

Rectangle

Declaration in Rectangle.pm:
package Rectangle;

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    $self->{width} = shift;
    $self->{height} = shift;
    return $self;
}
sub area {
    my $self = shift;
    return $self->{width} * $self->{height};
}
1;
Usage:
use Rectangle;
my $r = Rectangle->new(3, 10);
$r->{width} = 5;
print $r->area, "\n";
print ref($r), "\n";
print $r, "\n";

Object::Tiny

Declaration in Rectangle.pm:
package Rectangle;
use Object::Tiny qw(width height);

sub area {
    my $self = shift;
    return $self->width * $self->height;
}
1;
Usage:
use Rectangle;
my $r = Rectangle->new(width => 3,
                       height => 10);
print $r->area, "\n";
$r->{width} = 5;
print $r->area, "\n";

Moose

Moose Example

Declaration in Rectangle.pm:
package Rectangle;

use Moose;

has 'width'
    => ( is => 'rw', default => 0 );
has 'height'
    => ( is => 'rw', default => 0 );

sub area {
    my $self = shift;
    return $self->width * $self->height;
}

1;
Usage:
use Rectangle;
my $r = Rectangle->new(width => 3,
                       height => 10);
$r->width(5);
print $r->area, "\n";