title: How should I define a Moose object subroutine after its initialization?
body: <p>How should I define a Moose object subroutine after its initialization?</p>
  
  <p>I'm writing an object module using Moose and I plan to serialize (<code>nstore</code>) the created objects.</p>
  
  <p>Examine the following (simplified!) example:</p>
  
  <pre><code>package MyObj 0.001;
  
  use Moose;
  use namespace::autoclean;
  
  has 'size' =&gt; (
   is       =&gt; 'ro',
   isa      =&gt; 'Int',
   required =&gt; 1,
  );
  
  sub some_sub {
   my ($self, @more) = @_;
   if ($self-&gt;size() &lt; 100) # do something;
   elsif (($self-&gt;size() &lt; 500)) # do something else;
   elsif (($self-&gt;size() &lt; 7500)) # do something else;
   # ...
  }
  
  1;
  </code></pre>
  
  <p><code>some_sub</code> acts differently depending on <code>size</code>. Since <code>size</code> is read-only, it remains constant after the object has been initialized.</p>
  
  <p>So, assuming I call <code>some_sub</code> zillion times, it's a pity that I have to go through all the <code>if</code>s each time.</p>
  
  <p>I'd better do this once after the object has been initialized, then set <code>some_sub</code> to be a simpler function with no<code>if</code>s at all.</p>
  
  <p>But... how can I do that?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Perhaps I should add a <code>lazy</code> attribute of type subref that will hold a reference to the chosen subroutine. <code>some_sub</code> will then simply call <code>$self-&gt;chosen_sub-&gt;(@_)</code>. What do you think?</p>