Moose::Cookbook::Basics::BinaryTree\_BuilderAndLazyBuild(3) User Contributed Perl Documentation Moose::Cookbook::Basics::BinaryTree\_BuilderAndLazyBuild(3)

# [SYNOPSIS](#SYNOPSIS)

```
  package BinaryTree;
  use Moose;
  has 'node' => (is => 'rw', isa => 'Any');
  has 'parent' => (
      is        => 'rw',
      isa       => 'BinaryTree',
      predicate => 'has_parent',
      weak_ref  => 1,
  );
  has 'left' => (
      is        => 'rw',
      isa       => 'BinaryTree',
      predicate => 'has_left',
      lazy      => 1,
      builder   => '_build_child_tree',
  );
  has 'right' => (
      is        => 'rw',
      isa       => 'BinaryTree',
      predicate => 'has_right',
      lazy      => 1,
      builder   => '_build_child_tree',
  );
  before 'right', 'left' => sub {
      my ($self, $tree) = @_;
      $tree->parent($self) if defined $tree;
  };
  sub _build_child_tree {
      my $self = shift;
      return BinaryTree->new( parent => $self );
  }
```

# [DESCRIPTION](#DESCRIPTION)

If you've already read Moose::Cookbook::Basics::BinaryTree\_AttributeFeatures, then this example should look very familiar. In fact, all we've done here is replace the attribute's "default" parameter with a "builder".

In this particular case, the "default" and "builder" options act in exactly the same way. When the "left" or "right" attribute is read, Moose calls the builder method to initialize the attribute.

Note that Moose calls the builder method *on the object which has the* *attribute*. Here's an example:

```
  my $tree = BinaryTree->new();
  my $left = $tree->left();
```

When "$tree-&gt;left()" is called, Moose calls "$tree-&gt;\_build\_child\_tree()" in order to populate the "left" attribute. If we had passed "left" to the original constructor, the builder would not be called.

There are some differences between "default" and "builder". Notably, a builder is subclassable, and can be composed from a role. See Moose::Manual::Attributes for more details.

## [The lazy\_build shortcut](#The_lazy_build_shortcut)

The "lazy\_build" attribute option can be used as sugar to specify a whole set of attribute options at once:

```
  has 'animal' => (
      is         => 'ro',
      isa        => 'Animal',
      lazy_build => 1,
  );
```

This is a shorthand for:

```
  has 'animal' => (
      is        => 'ro',
      isa       => 'Animal',
      required  => 1,
      lazy      => 1,
      builder   => '_build_animal',
      predicate => 'has_animal',
      clearer   => 'clear_animal',
  );
```

If your attribute starts with an underscore, Moose is smart and will do the right thing with the "predicate" and "clearer", making them both start with an underscore. The "builder" method *always* starts with an underscore.

You can read more about "lazy\_build" in Moose::Meta::Attribute

# [CONCLUSION](#CONCLUSION)

The "builder" option is a more OO-friendly version of the "default" functionality. It also separates the default-generating code into a well-defined method. Sprinkling your attribute definitions with anonymous subroutines can be quite ugly and hard to follow.

2026-08-29 perl v5.44.0

---

Document: Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild(3pm) from perl-moose 2.4000-r4 (Chainguard OS)
Source: https://github.com/moose/Moose
