Zdeněk Žabokrtský & Rudolf Rosa
{zabokrtsky,rosa}@ufal.mff.cuni.cz
Tuesday 9.00–11.20
SU2
1. CPAN
2. Object Oriented Perl
The Comprehensive Perl Archive Network
A place where you can install to.
$ mkdir $HOME/BIG/perl
export PERL5LIB=$HOME/BIG/perl/lib/perl5:$HOME/BIG/perl/lib/perl5/i686-linux-thread-multi${PERL5LIB:+:$PERL5LIB} export PATH=$PATH:$HOME/BIG/perl/bin(should be added to .bashrc)
/afs/ms/u/z/zabokrtsky/BIG/.cpan
INSTALL_BASE=~/BIG/perl
--install_base ~/BIG/perl
o conf init connect_to_internet_ok urllist o conf commit
Two ways how to use cpan:
$ cpan -i Module::Starter
$ cpan cpan[1]> install Module::Starter
$ tar xzf Module.tgz
perl Makefile.PL make make test make install
perl Build.PL ./Build ./Build test ./Build install
Class::Std
by Damian Conway
bless
to make the reference
belong to a class
$object->method
,
subroutine method
from the object's class
(i.e. package) is called
man perlobj
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"; |
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"; |
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"; |
NPFL092 | Lecture 6 |