NAME
TheForce - Midichlorian-free Perl5 OOP
DESCRIPTION
Moose and Mouse are great. But how do they work? Normally people don't really care - they just work. TheForce is an extremely limited version which only supports has
and extends
at the moment, but still makes OOP in Perl5 extremely easy. What's best is everything is contained in just 3 small modules so you can see what is happening in the background, if you're interested. What's best is you get to use TheForce in every package ;-)
SYNOPSIS
# Foo.pm
package Foo;
use TheForce;
has 'x' => ( is => 'rw', isa => 'Int', default => 5 );
has 'greet' => ( is => 'ro', isa => 'Str', default => 'Hello, World!' );
sub sayHello {
my $self = shift;
say $self->greet;
}
1;
# Fooness.pm
package Fooness;
use TheForce;
extends 'Foo';
say Foo->x; # prints 5
Foo->x(7);
say Foo->x; # prints 7
my $foo = Foo->new;
$foo->sayHello(); # prints Hello, World!
METHODS
has
Creates an accessor for the particular package. Without arguments will return its value, or with arguments will set a new value. You can make the accessor read-only and set a specific type.
has 'x' => (
is => 'ro', # read only
isa => 'Int', # Integer only
default => 7, # default value
);
extends
Inherits the specified class.
package Foo;
extends 'MyPackage';
1;
force
Use the Force to push a subroutine into a class.
package JediPackage;
use TheForce;
has ( green => { is => 'ro', isa => 'Str', default => 'Yoda is green!' } );
package SithLord;
use TheForce;
extends 'JediPackage';
JediPackage->force( yoda => sub {
my $self = shift;
say $self->green;
});
my $jedi = JediPackage->new;
$jedi->yoda;
force_pull
Force pulls all the classes within the calling namespace.
package Jedi::Yoda;
use TheForce;
has 'yoda' => ( isa => 'Str', default => 'Inherited, am I' );
package Jedi::Obi;
use TheForce;
package Jedi;
use TheForce;
__PACKAGE__->force_pull; # inherits Jedi::Yoda and Jedi::Obi
say Jedi::Yoda->yoda;
AUTHOR
Brad Haywood <brad@geeksware.net>
LICENSE
You may distribute this code under the same terms as Perl itself.