GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Bread::Board::Manual::Example::LogDispatch(3) User Contributed Perl Documentation Bread::Board::Manual::Example::LogDispatch(3)

Bread::Board::Manual::Example::LogDispatch - An example of composing a dynamic Log::Dispatch object.

version 0.37

  my $c = container 'Logging' => as {
      service 'Logger' => (
          block => sub {
              my $s       = shift;
              my $c       = $s->parent;
              my $outputs = $c->get_sub_container('Outputs');
              my $log     = Log::Dispatch->new;
              foreach my $name ( $outputs->get_service_list ) {
                  $log->add(
                      $outputs->get_service( $name )->get
                  );
              }
              $log;
          }
      );

      container 'Outputs' => as {
          service 'File' => (
              block => sub {
                  Log::Dispatch::File->new(
                      name      => 'file',
                      min_level => 'debug',
                      filename  => 'logfile'
                  )
              }
          );
          service 'Screen' => (
              block => sub {
                  Log::Dispatch::Screen->new(
                      name      => 'screen',
                      min_level => 'warning',
                  )
              }
          );
      };
  };

  my $logger = $c->resolve( service => 'Logging/Logger' );

This example was inspired by a discussion I had with Jay Shirley. He wanted to know an easy way to have a dynamic list of output types for his Log::Dispatch object.

Often with Bread::Board you will be wiring up components that are of a fixed type and set, but this is not always the case. It is in these cases when you can simply use the Bread::Board objects themselves to fetch your dependencies.

The value passed into the block of a BlockInjection service is the service itself. Calling the "parent" method on that service will give you the container that service is in. From there you can introspect the other containers and services any which way you want to.

This example can be made even more dynamic if you build the 'Logging' component as a parameterized container whose parameter is the 'Outputs' container. Here is what that would look like.

  my $logging = container 'Logging' => [ 'Outputs' ] => as {
      service 'Logger' => (
          block => sub {
              my $s       = shift;
              my $c       = $s->parent;
              my $outputs = $c->get_sub_container('Outputs');
              my $log     = Log::Dispatch->new;
              foreach my $name ( $outputs->get_service_list ) {
                  $log->add(
                      $outputs->get_service( $name )->get
                  );
              }
              $log;
          }
      );
  };


  my $outputs = container 'Outputs' => as {
      service 'File' => (
          block => sub {
              Log::Dispatch::File->new(
                  name      => 'file',
                  min_level => 'debug',
                  filename  => 'logfile'
              )
          }
      );
      service 'Screen' => (
          block => sub {
              Log::Dispatch::Screen->new(
                  name      => 'screen',
                  min_level => 'warning',
              )
          }
      );
  };

  my $c  = $logging->create( Outputs => $outputs );
  my $ld = $c->resolve( service => 'Logging/Logger' );

This example illustrates how when a parameterized container is instantiated, the parameters become sub-containers of the resulting container. This makes it just as easy to fetch the 'Outputs' container and use it inside the 'Logger' service.

Stevan Little <stevan@iinteractive.com>

Please report any bugs or feature requests on the bugtracker website https://github.com/stevan/BreadBoard/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

This software is copyright (c) 2019, 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.

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

2019-06-28 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.