The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooseX::IOC - Moose attributes with IOC integration

SYNOPSIS

  # in a startup script somewhere ...

  use IOC;
  use IOC::Service::Parameterized;
  use IOC::Registry;
  use MooseX::IOC;

  {
      my $container = IOC::Container->new('MyProject');
      $container->register(IOC::Service::Literal->new('log_file' => "logfile.log"));
      $container->register(IOC::Service->new('FileLogger' => sub {
          my $c = shift;
          return FileLogger->new($c->get('log_file'));
      }));

      my $reg = IOC::Registry->new;
      $reg->registerContainer($container);
  }

  # in a .pm file somewhere ...

  package MyApplication;
  use Moose;

  has 'logger' => (
      metaclass => 'IOC',
      is        => 'ro',
      isa       => 'FileLogger',
      service   => '/MyProject/FileLogger',
  );

  # in a script file somewhere ...

  my $app = MyApplication->new;
  $app->logger; # automatically gotten from IOC

DESCRIPTION

This module provides a bridge between IOC registries and Moose objects through a custom attribute metaclass. It compliments the default option with a service option which contains a IOC::Registry path (and optional parameters).

The service option can be in one of the following formats:

IOC::Registry path string

This is the simplest version available, it is simply a path string which can be passed to IOC::Registry's locateService method.

IOC::Registry path string and parameters

This version is for use with IOC::Service::Parameterized services, and allows you to pass additional parameters to the locateService method. It looks like this:

  has 'logger' => (
      metaclass => 'IOC',
      is        => 'ro',
      isa       => 'FileLogger',
      service   => [ '/MyProject/FileLogger' => (log_file => 'foo.log') ],
  );
CODE reference

The last version is the most flexible, it is CODE reference which is expected to return an ARRAY ref similar to the above version.

  has 'logger' => (
      metaclass => 'IOC',
      is        => 'ro',
      isa       => 'FileLogger',
      lazy      => 1,
      service   => sub {
          my $self = shift;
          [ '/MyProject/FileLogger' => (
                log_file => $self->log_file
            ) ]
      },
  );

If the service is not found and a default option has been set, then it will return the value in default. This can be useful for writing code which can potentially be run both under IOC and not under IOC.

METHODS

meta

SEE ALSO

IOC
IOC::Registry
Moose

BUGS

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Stevan Little <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Copyright 2007-2009 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.