<meta http-equiv="content-type" content="text/html; charset=utf-8"><div>Awesome, thanks for the detailed feedback.</div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">

This means you want dependency injection.  Classes should *not* know how<br>to build things that they have; that's the job for some other part of<br>the program.</blockquote><div><br></div><div>Time to review my OO patterns!</div>

<div><br></div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">

Bread::Board is a Moose-based module for dependency injection.</blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">

... </blockquote><div><br></div>I'm going to start using it.<div><br clear="all">Regards,<br>Sean<br>
<br><div class="gmail_quote">On Sat, Apr 30, 2011 at 12:36 AM, Jonathan Rockway <span dir="ltr"><<a href="mailto:jon-chicagotalk@jrock.us">jon-chicagotalk@jrock.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">* On Fri, Apr 29 2011, Sean Blanton wrote:<br>
> Yeah, the 'Maybe' works great, and thanks for pointing me to the doc.<br>
><br>
> My ssh_builder looks like your safe_ssh, not sure what you meant. I<br>
> have one &ssh_builder for all (86!) classes. If a new class wants an<br>
> ssh connection, it consumes the 'SSH' role with the ssh attribute<br>
> populated by &ssh_builder.<br>
<br>
</div><br>
Bread::Board is a Moose-based module for dependency injection.  You can<br>
write something like this:<br>
<br>
    use Bread::Board;<br>
    my $c = container 'MyApp' as {<br>
        service 'ssh' => (<br>
            class     => 'Net::SSH',<br>
            lifecycle => 'Singleton', # if you want the same<br>
                                      # instance each time<br>
        );<br>
<br>
        service 'LogInAndDeleteEverything' => (<br>
            class        => 'MyApp::Action::LogInAndDeleteEverything',<br>
            dependencies => [ depends_on('ssh') ],<br>
        );<br>
    };<br>
<br>
    my $action = $c->resolve( service => 'LogInAndDeleteEverything' );<br>
    $action->execute; # everything is deleted<br>
<br>
Your MyApp::Action::LogInAndDeleteEverything would look something like:<br>
<br>
    package MyApp::Action::LogInAndDeleteEverything;<br>
    use Moose;<br>
<br>
    has 'ssh' => (<br>
        is => 'ro',<br>
        isa => 'Net::SSH',<br>
        required => 1,<br>
    );<br>
<br>
    sub execute { ... }<br>
<br>
Bread::Board also has support for type mapping, so theoretically there<br>
won't be much boilerplate even if you have 86 classes that all need the<br>
'ssh' service.  I haven't used this feature much, however.<br>
<br>
In addition to class injection, there is also block injection, where you<br>
can use a coderef to build the instance.  Sometimes convenient.<br>
<br>
It's also worth noting that services can have parameters and that you<br>
can pass in args during "resolve", so if your code looked something<br>
like:<br>
<br>
    has 'ssh' => ( ... as above ... );<br>
<br>
    has 'chdir_to' => ( ... );<br>
<br>
Your service could be changed to:<br>
<br>
    service 'LogInAndDeleteEverything' => ( # remind me never to use such<br>
                                            # a long name for an example!<br>
        class        => 'MyApp::Action::LogInAndDeleteEverything',<br>
        dependencies => [ depends_on('ssh') ],<br>
        parameters   => { chdir_to => { default => '/' } },<br>
    );<br>
<br>
Then, if you want to delete /etc, you would say:<br>
<br>
    my $action = $c->resolve(<br>
        service    => 'LogInAndDeleteEverything',<br>
        parameters => { chdir_to => '/etc' },<br>
    );<br>
    $action->execute; # BAI.<br>
<br>
Anyway, Bread::Board is pretty simple and easy to hack on, so it should<br>
be possible to do what you want without making a mess.<br>
<br>
One more thing: you can make diagrams of your services and your<br>
dependencies:<br>
<br>
<a href="https://github.com/jrockway/BreadBoard/commit/83de2f946b0af5a129146a085abfc95173ed0130" target="_blank">https://github.com/jrockway/BreadBoard/commit/83de2f946b0af5a129146a085abfc95173ed0130</a><br>
<br>
Finally, I used Bread::Board::Service as an example in the README here:<br>
<br>
<a href="https://github.com/jrockway/graphviz-hasa" target="_blank">https://github.com/jrockway/graphviz-hasa</a><br>
<br>
Pretty!<br>
<font color="#888888"><br>
--<br>
print just => another => perl => hacker => if $,=$"<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Chicago-talk mailing list<br>
<a href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/chicago-talk" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a></div></div></blockquote></div><br></div>