NAME

TheForce - Midichlorian-free Perl5 OOP

DESCRIPTION

TheForce is an extremely limited YAOOPF (Yet Another OOP Framework) to make the code easy to understand without jumping through many different classes and subroutines, but still makes OOP in Perl5 extremely easy. Everything is contained in just 3 small modules and runs very fast. What's best is you get to use TheForce in every package ;-) TheForce was not made to replace other OOP frameworks like Mo(o|u)se (or disappoint by being a YAOOPF), but as a learning exercise. And I like Star Wars.

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;    

before

Alters the subroutine to call the specified subroutine _before_ whatever is currently already set.

sub greet {
    say "Hello!";
}

before 'greet' => sub {
    say "I'm going to say hello...";
}

# when greet is called it outputs:
# I'm going to say hello...
# Hello!

after

The same as before but runs the new code _after_ the old subroutine code

AUTHOR

Brad Haywood <brad@geeksware.net>

LICENSE

You may distribute this code under the same terms as Perl itself.