Bread:...spatch(3pm)
π NAME π
Bread::Board::Manual::Example::LogDispatch β bestie, it's literally a whole example of composing a dynamic Log::Dispatch object. That's it, that's the vibe. π¦
β¨ VERSION β¨
version 0.37 πΈ
π SYNOPSIS π
okay so here's the whole thing, no cap:
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' );
likeβ¦ she's serving. π
π DESCRIPTION π
so this whole example was inspired by a discussion with Jay Shirley. he wanted to know an easy way to have a dynamic list of output types for his Log::Dispatch object. and honestly? relatable.
often with Bread::Board you'll be wiring up components that are of a fixed type and set β super normal, super chill β but that is not always the case. and it's in those exact cases that you can simply use the Bread::Board objects themselves to fetch your dependencies. the way that works?? iconic.
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. go off, bestie. π
and listen β 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' );
and then, the little life lesson this example is illustrating: when a parameterized container is instantiated, the parameters become sub-containers of the resulting container. so it's just as easy to fetch the 'Outputs' container and use it inside the 'Logger' service. literally so clean, we're obsessed. π«Ά
πΈ AUTHOR πΈ
Stevan Little <stevan@iinteractive.com> π
π BUGS π
pls pls pls report any bugs or feature requests on the bugtracker website:
https://github.com/stevan/BreadBoard/issues
and when you're submitting a bug or request, bestie, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. we love a reproducible moment. π
π COPYRIGHT AND LICENSE π
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. slay. β
okay that's the whole page, mwah π β this was perl v5.36.0, dated 2022-12-12, and it's the Bread:...spatch(3pm) page from your User Contributed Perl Documentation. go resolve some services, you got this ππ¦
π¦ where this page comes from
- debian bookworm: libbread-board-perl 0.37-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libbread-board-perl 0.37-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libbread-board-perl 0.37-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libbread-board-perl 0.37-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms