title: How to set ro attributes during build?
body: <p>I'm writing a script to help me get proficient in Moose. I've got the following bit of code:</p>
  
  <pre><code>package Dir;
  use Moose;
  use Modern::Perl;
  use File;
  
  has 'dirs' =&gt;             (is =&gt; 'ro', isa =&gt; 'HashRef[Dir]' );  
  has 'files' =&gt;            (is =&gt; 'ro', isa =&gt; 'HashRef[File]'); 
  has 'dir_class' =&gt;        (is =&gt; 'ro', isa =&gt; 'ClassName', default =&gt; 'Dir');
  has 'file_class' =&gt;       (is =&gt; 'ro', isa =&gt; 'ClassName', default =&gt; 'File');
  
  sub BUILD {
    my $self = shift;
    my $path = $self-&gt;path;
    my $name = $self-&gt;name;
    my (%dirs, %files);
  
    # populate dirs attribute with LaborData::Data::Dir objects
    opendir my $dh, $path or die "Can't opendir '$path': $!";
  
    # Get files and dirs and separate them out
    my @dirs_and_files = grep { ! m{^\.$|^\.\.$} } readdir $dh;
    closedir $dh or die "Can't closedir '$path': $!";
    my @dir_names         = grep { -d "$path/$_" } grep { !m{^\.}  } @dirs_and_files;
    my @file_names        = grep { -f "$path/$_" } grep { !m{^\.}  } @dirs_and_files;
  
    # Create objects
    map { $dirs{$_}         = $self-&gt;dir_class-&gt;new  ( path =&gt; $path . '/' . $_ ) } @dir_names;
    map { $files{$_}        = $self-&gt;file_class-&gt;new ( path =&gt; $path . '/' . $_ ) } @file_names;
  
    # Set attributes
    $self-&gt;dirs         ( \%dirs );
    $self-&gt;files        ( \%files );
  }
  </code></pre>
  
  <p>The code results in the following error: <code>died: Moose::Exception::CannotAssignValueToReadOnlyAccessor (Cannot assign a value to a read-only accessor at reader Dir::dirs</code></p>
  
  <p>To get around this error, I could either make the attributes <code>rw</code> or use <code>builder</code> methods for the <code>dirs</code> and <code>files</code> attributes. The former solution is undesirable and the latter solution will require duplication of code (for example, the directory will need to be opened twice) and so is also undesirable.</p>
  
  <p>What is the best solution to this problem?</p>