Soo - Simple object oriented system for Perl
version 0.0.4
In Person.pm:
package Person; use Soo; has 'name'; # has name => {}; is valid also has age => { rw => 1, default => 0 }; # age can change 1;
In Employee.pm:
package Employee; use Soo; extends 'Person'; # or use parent 'Person'; has 'id'; has email => { # set is used to change received values # here we will make sure that email will be always lowercase set => sub { my $self = shift; lc(shift); } }; 1;
In example.pl:
use Employee; # constructor params must be in a hash ref my $obj = Employee->new({ name => "Gabi", id => "123", email => "GABI@FOLLOW.ME" }); $obj->name; # Gabi $obj->name('Gisele'); $obj->name; # Gabi - the name remains the same, because every method is by default readonly $obj->id; # 123 $obj->age; # 0 $obj->age(19); $obj->age; # 19 - age method was defined readwrite $obj->email; # gabi@follow.me # the email was specified in uppercase in the constructor # but the set function changed it to lowercase
A tiny module that provides some sugars for object oriented programming in Perl. A list of characteristics:
compatible with bless stuff
can be mixed with your own methods declared with sub {}
can be imported with: use parent 'MyModule'
generates read only methods by default
generates read write methods if specified
new takes a hash reference as param
new
does not inherit from a special base class
use strict; use warnings; package Pet; use Soo; has eat => { default => 'eating' }; has fly => { default => 'flying' }; has 'name'; has run => { default => 'running' }; has talk => { default => 'talking' }; has sleep => { default => 'sleeping' }; package Pet::Cat; use Soo; extends 'Pet'; has fly => { default => 'I cannot fly' }; has talk => { default => 'meow' }; package Pet::Dog; use Soo; extends 'Pet'; has fly => { default => 'I cannot fly' }; has talk => { default => 'wow' }; package Pet::Parrot; use Soo; extends 'Pet'; has run => { default => 'I cannot run' }; has talk => { default => 'argh' }; package main; my $cat = Pet::Cat->new({ name => 'Simba' }); my $dog = Pet::Dog->new({ name => 'Buddy' }); my $parrot = Pet::Parrot->new({ name => 'Petey' }); $cat->name; # Simba $cat->eat; # eating $cat->fly; # I cannot fly $cat->run; # running $cat->talk; # meow $cat->sleep; # sleeping $dog->name; # Buddy $dog->eat; # eating $dog->fly; # I cannot fly $dog->run; # running $dog->talk; # wow $dog->sleep; # sleeping $parrot->name; # Petey $parrot->eat; # eating $parrot->fly; # flying $parrot->run; # I cannot run $parrot->talk; # argh $parrot->sleep; # sleeping
Please report any bugs or feature requests through the issue tracker at https://github.com/geovannyjs/soo/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/geovannyjs/soo
git clone https://github.com/geovannyjs/soo.git
Geovanny Junio <geovannyjs@gmail.com>
To install Soo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Soo
CPAN shell
perl -MCPAN -e shell install Soo
For more information on module installation, please visit the detailed CPAN module installation guide.