So do_n(), the builder function, fires, then do_n2() fires "after". However, while do_n2() is provided with the object instance this instance does not contain the value for n which was initialized by the builder

Pretty much any time you want the builder/default of one attribute to have access to the values of another attribute, you want to use lazy. The order of attribute initialization in Moose (and Mouse) is undefined (basically hash-order). When you use lazy this allows the order to essentially be determined by the dependency chain itself. What you are doing here is pretty much more easily done like this:

#!/usr/bin/perl -w use strict; package Test; use Mouse; has 'n' => (is => 'rw', isa => 'Int', lazy => 1, builder => 'do_n'); has 'n2' => (is => 'rw', isa => 'Int', lazy => 1, builder => 'do_n2'); sub do_n { my $s = shift; print "In do_n() \$s = $s\n"; return 3; } sub do_n2 { my $s = shift; print "In do_n2() \$s = $s\n"; return $s->n + 2; } __PACKAGE__->meta->make_immutable(); package main; use Data::Dumper; my $test = Test->new(); warn Dumper $test; $test->n2; warn Dumper $test;
which produces the following output:
$VAR1 = bless( {}, 'Test' ); In do_n2() $s = Test=HASH(0x861360) In do_n() $s = Test=HASH(0x861360) $VAR1 = bless( { 'n' => 3, 'n2' => 5 }, 'Test' );

-stvn

In reply to Re: Simple Mouse/Moose question by stvn
in thread Simple Mouse/Moose question by halfcountplus

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.