Hello Monks,

I find myself using this design pattern with moose. Obviously, this is an abbreviated example, but I find myself needing "sub" objects regularly. How do you deal with passing required attributes through?

package My::App; use 5.010; use Moose; use strict; use warnings; use MooseX::HasDefaults::RO; has [qw( foo bar )] => ( isa => 'Int', required => 1, ); has [qw( biz baz )] => ( isa => 'Int', default => '1', ); package My; use 5.010; use Moose; use strict; use warnings; use MooseX::HasDefaults::RO; has [qw(app_foo app_bar)] => ( isa => 'Int', required => 1, ); has [qw(app_biz app_baz)] => ( isa => 'Int', required => 0, ); has 'MyApp' => ( lazy => 1, isa => 'My::App', default => sub { my $attrs = [qw( biz baz )]; my %opt_params; foreach my $attribute (@$attrs) { my $method_name = 'app_' . $attribute; $opt_params{$attribute} = $_[0]->$method_name if $_[0]->$m +ethod_name; } My::App->new( foo => $_[0]->app_foo, bar => $_[0]->app_bar, %opt_params, ); }, handles => qr/^(.*)/, ); package main; use 5.010; use strict; use warnings; use Test::More; #BEGIN { use_ok 'My' } my $app = new_ok 'My' => [ app_foo => 1, app_bar => 2, app_biz => 3, ]; foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz +)){ is $app->$test, $app->$test, '$app' . "->" . $test; } is $app->foo, 1, "foo ok"; is $app->app_foo, 1, "app_foo ok"; # Not sure what I was going for here... it started out as the followin +g but that didn't work # is $app->$test, $count++ %4 ? $app->$test : undef , '$app' . "->" . +$test my $count = 0; foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz +)){ is $app->$test, $count++ %4 ? $app->$test : $app->$test , '$app' . + "->" . $test; }

Obviously, in the real world I wouldn't be testing the output with Test::More and would likely be making decisions based on those values obtained from $obj->$value. I tried

there's a gist here of the same thing...ish... (I modified it for some reason... but it's minimal... actually I think the example here may be more modified than the gist...)


In reply to [Moose] Critique request on my Moose design (anti?)pattern by three18ti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.