NAME

Juice - Live on the edge; share methods and variables between packages easily.

VERSION

Version 0.01

SYNOPSIS

Juice provides a way to share methods and variables between packages. No exporting, no inheritance, no OO programming. Probably not the best practice, but if you want a quick fix and don't care about the quality of your code, then Juice it!

## MyJuicePackage.pm
package MyJuicePackage;

use Juice::Zest; # imports use strict/warnings and 5.010;
use Juice;

# set a variable called 'x' that can be called ANYWHERE
# using a special Juice function
has [{ x => 5 }];

def 'add_to_x' => sub {
    has [{ x => _s('x')+1 }];
    say "Added 1 to x";
};
1;

## test.pl
use Juice::Zest;
use Juice;

use MyJuicePackage; # load our newly created package

say "x = " . _s('x');

_x 'add_to_x';

say "x = " . _s('x');

Running the above returns: x = 5 Added 1 to x x = 6

squeeze Adds a 'new' constructor to your package on the fly. If you pass arguments to it in the form of a hash, then it will bless them for you automatically.

package MyApp;

use Juice;

squeeze({ name => 'foo' });

sub get_name {
    my $self = shift;
    
    return $self->{name};
}
1;

# test.pl
use MyApp;

my $j = MyApp->new;
print "Hello, $j->{name}\n"; # returns 'Hello, foo'

func Creates an anonymous subroutine in $function->{func_name} This allows you to run the function from anywhere using _x(func_name)

func 'mysub' => sub {
    say "Hello, World!";
};

_x 'mysub';

# outputs:
# Hello, World!

The above example can be called from the current package, or any other packages using it.

def A shortcut for func (by 1 letter!). Originally I used func as this method, but decided I liked def better. You can use either one - they do the same thing.

_x Run a Juice function with/without arguments

_x 'mysub'; # without arguments
_x 'mysub', 'hello', [qw/1 2 3 4 5/]; # with arguments

is_juiced Returns 1 or 0 whether a function or variable has been 'juiced'. This means is it a def, func or has.

has [{ x => 5 }];
my $y = 7;

if (is_juiced('x')) { say "Hooray!"; }
if (! is_juiced('y')) { say "Boo!"; }

# output:
# Hooray!
# Boo!

has No. This is nothing like Moose. Juice's has acts as a variable that can hold any given type and be accessed from absolutely anywhere; packages, subroutines, loops. You can set multiple variables in one go, or a single one. You may also give it a type, so if the ref does not match it will throw out errors. All has definitions need to be referenced by _s

has [{ name => 'foo' }]; # a simple example
has [{ name => 'foo' }, { food => 'pizza' }]; # setting two variables in one instance

say _s('name'); # returns 'foo'

# you can also create anonymous subs out of them
has [{
    mysub => sub {
        my $name = shift;
        say "Hello, $name!";
    }
}];

_s('mysub')->('World'); # prints 'Hello, World!'

loop Just another way to perform a loop. Nothing special, really. It allows you to use callbacks for the data, or an inline sub

loop [qw/1 2 3/], sub {
    say shift;
};

# output:
# 1
# 2
# 3

# or via a callback

has [{
    cb => sub {
        my $animal = shift;
        say "On the farm there is a $animal";
    }
}];

loop [qw/Donkey Cow Horse Camel/], _s('cb');
has [{ foo => 'bar' }];
say _s 'foo'; # return 'foo'

AUTHOR

Brad Haywood, <brad at geeksware.net>

BUGS

Please report any bugs or feature requests to bug-juice at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Juice. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Juice

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011 Brad Haywood.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 246:

Unknown directive: =head