package MooseX::Service; use Moose::Role; use Carp; our $VERSION = 0.01; sub register { my ($self,$service,$coderef) = @_; croak "register expects a code reference as second argument" unless ref $coderef eq 'CODE'; $self->meta->add_attribute ($service => { reader => $service, default => sub { $coderef->($self) }, lazy => 1, }); return; } 1; __END__ =pod =head1 NAME MooseX::Service - Turns your class into a lazy service locator =head1 SYNOPSIS package Foo::Service; use Moose; with qw/MooseX::Service/; 1; # ...later, in a different country my $service = Foo::Service->new; $service->register (hello => sub { "Hello " . $_[0]->world }); $service->register (world => sub { "world!\n" }); print $service->hello; # Prints "Hello world!\n"; =head1 DESCRIPTION =over4 =item B A lazy service locator is an object which you can install services on which will only be created when they are actually needed. This has the added bonus that services can be added out of order and will be initialized in the correct order, provided there are no cyclic dependencies. =head1 METHODS =over4 =item B $coderef)> Adds $coderef provided as a method called $name on the service object. =head1 ACKNOWLEDGEMENTS =over4 =item Stevan Little for making Moose and directing me into the world of meta-foo. =head1 SEE ALSO =over4 =item L =item The #moose channel on irc.perl.org =head1 BUGS Like every module, there are bound to be some bugs. If you find any, please either email me or add the bug to cpan-RT. =head1 AUTHOR Anders Nor Berle Edebolaz@gmail.comE =head1 COPYRIGHT AND LICENSE Copyright 2007 by Anders Nor Berle. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut