Beam::Wire - A Dependency Injection Container
# wire.yml dbh: class: 'DBI' args: - 'dbi:mysql:dbname' - { PrintError: 1 } cache: class: 'CHI' args: driver: 'DBI' dbh: { ref: 'dbh' } # myscript.pl use Beam::Wire; my $wire = Beam::Wire->new( file => 'wire.yml' ); my $dbh = $wire->get( 'dbh' ); my $cache = $wire->get( 'cache' ); $wire->set( 'dbh', DBI->new( 'dbi:pgsql:dbname' ) );
Beam::Wire is a dependency injection container.
TODO: Explain what a DI container does and why you want it
Read the list of services from the given file. The file is described below in the FILE section.
Create a new container.
Beam::Wire can read a YAML file to fill a container with services. The file should be a single hashref. The keys will be the service names.
The class to instantiate. The class will be loaded and the new method called.
new
The arguments to the new method. This can be either an array or a hash, like so:
# array dbh: class: DBI args: - 'dbi:mysql:dbname' # hash cache: class: CHI args: driver: Memory max_size: 16MB
Using the array of arguments, you can give arrayrefs or hashrefs:
# arrayref of arrayrefs names: class: 'Set::CrossProduct' args: - - [ 'Foo', 'Barkowictz' ] - [ 'Bar', 'Foosmith' ] - [ 'Baz', 'Bazleton' ] # hashref cache: class: CHI args: - driver: Memory max_size: 16MB
Beam::Wire objects can hold other Beam::Wire objects!
inner: class: Beam::Wire args: config: dbh: class: DBI args: - 'dbi:mysql:dbname' cache: class: CHI args: driver: Memory max_size: 16MB
Inner containers' contents can be reached from outer containers by separating the names with a slash character:
my $dbh = $wire->get( 'inner/dbh' );
To install Beam::Wire, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Beam::Wire
CPAN shell
perl -MCPAN -e shell install Beam::Wire
For more information on module installation, please visit the detailed CPAN module installation guide.