programació orientada a objectes en perl

11

Click here to load reader

Upload: alex-muntada

Post on 11-Jun-2015

376 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Programació orientada a objectes en Perl

Programació OO en Perl

Curs d'introducció a Perl 2011

Barcelona Perl Mongers

Alex Muntada <[email protected]>

Moose

El sistema d'objectes de Perl 5

Simple, bàsic, esquelètic, flexible.

Ofereix poca ajuda per a la programació orientada a objectes.

Moose

Moose és un sistema d'objectes complet ipotent

Està construït al damunt del sistema d'objectes de Perl 5.

Pren característiques d'altres llenguatges com Smalltalk, Common Lisp iPerl 6.

Moose és la millor manera de programar objectes en Perl 5 modern.

ClassesEn Perl 5 clàssic una classe és un paquet (o mòdul) de Perl dins un fitxer:Cat.pm

package Cat; use Moose; 1;

1 de 11

Page 2: Programació orientada a objectes en Perl

ClassesEn Perl 5 modern és el mateix però amb una sintaxi alternativa que permetcrear un context més específic que el del fitxer.

Això ens interessa en cas que vulguem posar més d'una classe dins el mateixfitxer.

{ package Cat; use Moose; }

Mètodes use Cat;

my $brad = Cat->new(); my $jack = Cat->new();

Mètodes use Cat; Cat->meow();

my $alarm = Cat->new(); $alarm->meow();

Mètodes { package Cat; use Moose; sub meow { my $self = shift; say "Meow!";

2 de 11

Page 3: Programació orientada a objectes en Perl

} }

Atributs { package Cat; use Moose; has 'name' => ( is => 'ro', isa => 'Str', );

has( 'age', is, 'ro' ); has 'diet', is => 'rw'; }

Atributs use Cat; my $fat = Cat->new( name => 'Fatty', age => 8, diet => 'Sea Treats' ); say $fat->name(), ' eats ', $fat->diet();

$fat->diet( 'Low Sodium Kitty Lo Mein' ); say $fat->name(), ' now eats ', $fat->diet();

Encapsulament { package Cat; use Moose;

has 'name', is => 'ro', isa => 'Str'; has 'diet', is => 'rw'; has 'birth_year', is => 'ro', isa => 'Int', default => sub { (localtime)[5] + 1900 };

3 de 11

Page 4: Programació orientada a objectes en Perl

Encapsulament sub age { my $self = shift; my $year = (localtime)[5] + 1900; return $year - $self->birth_year(); } }

Polimorfisme sub show_vital_stats { my $object = shift;

say 'My name is ', $object->name(); say 'I am ', $object->age(); say 'I eat ', $object->diet(); }

PolimorfismeDuck Typing

Si quelcom fa quack() és que és un ànec.

Polimorfisme # how old is the cat? my $years = $zeppie->age(); # store the cheese in the warehouse for six months $cheese->age();

Rols {

4 de 11

Page 5: Programació orientada a objectes en Perl

package LivingBeing;use Moose::Role;

requires qw( name age diet ); }

Rols { package Cat; use Moose; has 'name', is => 'ro', isa => 'Str'; has 'diet', is => 'rw', isa => 'Str'; has 'birth_year', is => 'ro', isa => 'Int', default => (localtime)[5] + 1900;

with 'LivingBeing';

sub age { ... } }

Rols { package Cat; use Moose; has 'name', is => 'ro', isa => 'Str'; has 'diet', is => 'rw'; with 'LivingBeing', 'CalculateAge::From::BirthYear'; }

Rols use Cat;

my $kitten = Cat->new( name => 'Kitty', diet => 'fish' );

say 'This Cat is alive!'

5 de 11

Page 6: Programació orientada a objectes en Perl

if $kitten->DOES( 'LivingBeing' );

Herència { package LightSource; use Moose;

has 'candle_power', is => 'ro', isa => 'Int', default => 1;

has 'enabled', is => 'ro', isa => 'Bool', default => 0, writer => '_set_enabled';

Herència sub light { my $self = shift; $self->_set_enabled(1); }

sub extinguish { my $self = shift; $self->_set_enabled(0); } }

Herència { package LightSource::SuperCandle; use Moose;

extends 'LightSource';

has '+candle_power', default => 100; }

6 de 11

Page 7: Programació orientada a objectes en Perl

Herència { package LightSource::Glowstick; use Moose;

extends 'LightSource';

sub extinguish { } }

Herència { package LightSource::Cranky; use Carp; use Moose;

extends 'LightSource';

Herència override light => sub { my $self = shift; Carp::carp( "Can't light a lit light source!" ) if $self->enabled;

super(); };

Herència override extinguish => sub { my $self = shift; Carp::carp( "Can't extinguish an unlit light source!" ) unless $self->enabled;

7 de 11

Page 8: Programació orientada a objectes en Perl

super(); }; }

Herència say 'Looks like a LightSource' if $sconce->isa( 'LightSource' );

say 'Monkeys do not glow' unless $chimpy->isa( 'LightSource' );

Moose i OO en Perl 5 my $metaclass = Monkey::Pants->meta(); say 'Monkey::Pants instances have the attributes:'; say $_->name for $metaclass->get_all_attributes; say 'Monkey::Pants instances support the methods:'; say $_->fully_qualified_name for $metaclass->get_all_methods; # You can even see which classes extend a given class my $metaclass = Monkey->meta(); say 'Monkey is the superclass of:'; say $_ for $metaclass->subclasses;

Referències d'objectes { package Player;

sub new { my ($class, %attrs) = @_; return bless \%attrs, $class; } }

8 de 11

Page 9: Programació orientada a objectes en Perl

Referències d'objectes my $joel = Player->new( number => 10, position => 'center', );

my $jerryd = Player->new( number => 4, position => 'guard', );

Referències d'objectes sub format { my $self = shift; return '#' . $self->{'number'} . ' plays ' . $self->{'position'}; } sub number { return shift->{'number'} } sub position { return shift->{'position'} }

Cerca de mètodes i herència package InjuredPlayer; use parent qw( Player Hospital::Patient ); use mro 'c3';

AUTOLOAD { package Foobar; sub AUTOLOAD { our $AUTOLOAD;

9 de 11

Page 10: Programació orientada a objectes en Perl

my ($name) = $AUTOLOAD =~ /::(\w+)$/;

# pretty-print the argumentslocal $" = ', ';

say "In AUTOLOAD(@_) for $name!" } }

Sobrecàrrega de mètodes sub overridden { my $self = shift; return $self->SUPER::overridden( @_ ); }

Reflexió say "$pkg exists" if eval { $pkg->can( 'can' ) };

sub module_loaded { ny $modname = shift; $modname =~ s!::!/!g;

return exists $INC{ $modname . '.pm' }; } say "$func() exists" if $pkg->can( $func );

Conceptes avançatsAfavoriu la composició abans que l'herència.

Principi de responsabilitat única: una classe no ha de fer més feinade la que li correpon (Empleat sap d'empleats no de nòmines).

DRY: Don¿t Repeat Yourself.

Principi de substitució de Liskov: accepteu generalitzacions però

10 de 11

Page 11: Programació orientada a objectes en Perl

sigueu específics en el que retorneu.

Subtipus i coercions.

Immutabilitat: proveu a fer els vostres objectes no modificables.

GràciesPreguntes? Dubtes? Comentaris? Idees?

11 de 11