NAME
Sub::Lib - Stuff sub-routines into a run-time namespace. Because. Reasons.
SYNOPSIS
use Sub::Lib;
# create a library
my $lib = Sub::Lib->new({
'log' => sub {print join(' ', localtime. ':', @_), "\n"},
});
# add methods
$lib->('info', sub {$lib->('log')->('info:', @_)});
$lib->('warn', sub {$lib->('log')->('warn:', @_)});
# call them directly
$lib->('info')->('This is for information');
# or via some sugar
$lib->run('warn', 'This is for warnings');
# or via some oo sugar
$lib->('method', sub {my ($self, @args) = @_; $self->run(@args);});
$lib->call('method', $lib, 'info', "Have you seen? Oh I've seen.");
# cheeseburger
{
my $sub = $lib->has('warn');
$sub->("I can has.")
if $sub;
}
# scan the library
$lib->('info')->('installed subs:', join(', ', keys %{$lib->()}));
DESCRIPTION
Sub::Lib allows you to store sub-routines into a common library which can then passed around as a variable. It's a run-time namespace.
USAGE
new([HASHREF | LIST])
Creates a library object and initializes it with entries that may be passed in as either a HASH
reference or LIST
of key-value pairs. The object created is itself a sub-routine that can be called directly in order to run sub-routines stored in the library:
$lib->('sub-routine name goes here')->(qw(sub routine args go here));
Additional sub-routines may be added by providing a CODE
reference:
$lib->('a new sub-routine', sub {
# code goes here
});
If no arguments are passed, the internal library is returned:
my $_lib = $lib->();
has($name)
Returns the sub-routine installed in the library identified by $name
or undef if it does not exist.
run($name, [LIST])
Runs the sub-routine stored in the library identified by $name
. An exception will be thrown if no sub-routine by that name can be found. Any additional arguments are passed to the sub-routine.
call($object, $name, [LIST])
Calls the sub-routine stored in the library identified by $name
as a method to the object in $object
. This is similar to run()
above but uses Perl's object semantics. Additional arguments are passed to the method.
AUTHOR
jason hord <pravus@cpan.org>
LICENSE
This software is information. It is subject only to local laws of physics.