DESCRIPTION

Bread::Board is an inversion of control framework with a focus on dependency injection and lifecycle management. It's goal is to help you write more decoupled objects and components by removing the need for you to manually wire those objects/components together.

Loading this package will automatically load the rest of the packages needed by your Bread::Board configuration.

EXPORTED FUNCTIONS

The functions of this package provide syntactic sugar to help you build your Bread::Board configuration. You can build such a configuration by constructing the objects manually instead, but your code may be more difficult to understand.

container

simple case

container $name, \&body;

This function constructs and returns an instance of Bread::Board::Container. The (optional) &body block may be used to add services or sub-containers within the newly constructed container. Usually, the block is not passed directly, but passed using the as function.

If $name starts with '+', and the container is being declared inside another container, then this declaration will instead extend an existing container with the name $name (without the '+').

from an instance

container $container_instance, \&body

In many cases, subclassing Bread::Board::Container is the easiest route to getting access to this framework. You can do this and still get all the benefits of the syntactic sugar for configuring that class by passing an instance of your container subclass to container.

with parameters

container $name, \@parameters, \&body

A third way of using the container function is to build a parameterized container. These are useful as a way of providing a placeholder for parts of the configuration that may be provided later. You may not use an instance object in place of the $name in this case.

The type of injection performed depends on the parameters used. You may use the service_class parameter to pick a specific injector class. For instance, this is useful if you need to use Bread::Board::SetterInjection or have defined a custom injection service. If you specify a block, block injection will be performed using Bread::Board::BlockInjection. If neither of these is present, constructor injection will be used with Bread::Board::ConstructorInjection (and you must provide the class option).

service dependencies

The dependencies parameter takes a hashref of dependency names mapped to Bread::Board::Dependency objects, but there are several coercions and sugar functions available to make specifying dependencies as easy as possible. The simplest case is when the names of the services you're depending on are the same as the names that the service you're defining will be accessing them with. In this case, you can just specify an arrayref of service names:

service foo => (
dependencies => [ 'bar', 'baz' ],
# ...
);

If you need to use a different name, you can specify the dependencies as a hashref instead:

service foo => (
dependencies => {
dbh => 'foo_dbh',
},
# ...
);

You can also specify parameters when depending on a parameterized service:

inheriting and extending services

If the $name starts with a '+', the service definition will instead extend an existing service with the given $name (without the '+'). This works similarly to the has '+foo' syntax in Moose. It is most useful when defining a container class where the container is built up in BUILD methods, as each class in the inheritance hierarchy can modify services defined in superclasses. The dependencies and parameters options will be merged with the existing values, rather than overridden. Note that literal services can't be extended, because there's nothing to extend. You can still override them entirely by declaring the service name without a leading '+'.

depends_on

wire_names

wire_names(@service_names);

This function is just a shortcut for passing a hash reference of dependencies into the service. It is not typically needed, since Bread::Board can usually understand what you mean - these declarations are all equivalent:

infer

infer;
infer(%hints);

This is used with typemap to help create the typemap inference. It can be used with no arguments to do everything automatically. However, in some cases, you may want to pass a service instance as the argument or a hash of service arguments to change how the type map works. For example, if your type needs to be constructed using a setter injection, you can use an inference similar to this:

include

This is a shortcut for loading a Bread::Board configuration from another file.

include "filename.pl";

The above is pretty much identical to running:

do "filename.pl";

However, you might find it more readable to use include.

alias

alias $service_name, $service_path, %service_description;

This helper allows for the creation of service aliases, which allows you to define a service in one place and then reuse that service with a different name somewhere else. This is sort of like a symbolic link for services. Aliases will be resolved recursively, so an alias can alias an alias.

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.