Bread:...spatch(3pm)

πŸ“¦
version 0.37 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu questing

πŸ’– 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. πŸ™ˆ


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