.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Moose::Cookbook::Basics::BinaryTree_AttributeFeatures 3" .TH Moose::Cookbook::Basics::BinaryTree_AttributeFeatures 3 2026-06-15 "perl v5.42.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Moose::Cookbook::Basics::BinaryTree_AttributeFeatures \- Demonstrates various attribute features including lazy, predicates, weak refs, and more .SH VERSION .IX Header "VERSION" version 2.4000 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& package BinaryTree; \& use Moose; \& \& has \*(Aqnode\*(Aq => ( is => \*(Aqrw\*(Aq, isa => \*(AqAny\*(Aq ); \& \& has \*(Aqparent\*(Aq => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqBinaryTree\*(Aq, \& predicate => \*(Aqhas_parent\*(Aq, \& weak_ref => 1, \& ); \& \& has \*(Aqleft\*(Aq => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqBinaryTree\*(Aq, \& predicate => \*(Aqhas_left\*(Aq, \& lazy => 1, \& default => sub { BinaryTree\->new( parent => $_[0] ) }, \& trigger => \e&_set_parent_for_child \& ); \& \& has \*(Aqright\*(Aq => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqBinaryTree\*(Aq, \& predicate => \*(Aqhas_right\*(Aq, \& lazy => 1, \& default => sub { BinaryTree\->new( parent => $_[0] ) }, \& trigger => \e&_set_parent_for_child \& ); \& \& sub _set_parent_for_child { \& my ( $self, $child ) = @_; \& \& confess "You cannot insert a tree which already has a parent" \& if $child\->has_parent; \& \& $child\->parent($self); \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This recipe shows how various advanced attribute features can be used to create complex and powerful behaviors. In particular, we introduce a number of new attribute options, including \f(CW\*(C`predicate\*(C'\fR, \f(CW\*(C`lazy\*(C'\fR, and \f(CW\*(C`trigger\*(C'\fR. .PP The example class is a classic binary tree. Each node in the tree is itself an instance of \f(CW\*(C`BinaryTree\*(C'\fR. It has a \f(CW\*(C`node\*(C'\fR, which holds some arbitrary value. It has \f(CW\*(C`right\*(C'\fR and \f(CW\*(C`left\*(C'\fR attributes, which refer to its child trees, and a \f(CW\*(C`parent\*(C'\fR. .PP Let\*(Aqs take a look at the \f(CW\*(C`node\*(C'\fR attribute: .PP .Vb 1 \& has \*(Aqnode\*(Aq => ( is => \*(Aqrw\*(Aq, isa => \*(AqAny\*(Aq ); .Ve .PP Moose generates a read\-write accessor for this attribute. The type constraint is \f(CW\*(C`Any\*(C'\fR, which literally means it can contain anything. .PP We could have left out the \f(CW\*(C`isa\*(C'\fR option, but in this case, we are including it for the benefit of other programmers, not the computer. .PP Next, let\*(Aqs move on to the \f(CW\*(C`parent\*(C'\fR attribute: .PP .Vb 6 \& has \*(Aqparent\*(Aq => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqBinaryTree\*(Aq, \& predicate => \*(Aqhas_parent\*(Aq, \& weak_ref => 1, \& ); .Ve .PP Again, we have a read\-write accessor. This time, the \f(CW\*(C`isa\*(C'\fR option says that this attribute must always be an instance of \&\f(CW\*(C`BinaryTree\*(C'\fR. In the second recipe, we saw that every time we create a Moose\-based class, we also get a corresponding class type constraint. .PP The \f(CW\*(C`predicate\*(C'\fR option is new. It creates a method which can be used to check whether or not a given attribute has been initialized. In this case, the method is named \f(CW\*(C`has_parent\*(C'\fR. .PP This brings us to our last attribute option, \f(CW\*(C`weak_ref\*(C'\fR. Since \&\f(CW\*(C`parent\*(C'\fR is a circular reference (the tree in \f(CW\*(C`parent\*(C'\fR should already have a reference to this one, in its \f(CW\*(C`left\*(C'\fR or \f(CW\*(C`right\*(C'\fR attribute), we want to make sure that we weaken the reference to avoid memory leaks. If \f(CW\*(C`weak_ref\*(C'\fR is true, it alters the accessor function so that the reference is weakened when it is set. .PP Finally, we have the \f(CW\*(C`left\*(C'\fR and \f(CW\*(C`right\*(C'\fR attributes. They are essentially identical except for their names, so we\*(Aqll just look at \&\f(CW\*(C`left\*(C'\fR: .PP .Vb 8 \& has \*(Aqleft\*(Aq => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqBinaryTree\*(Aq, \& predicate => \*(Aqhas_left\*(Aq, \& lazy => 1, \& default => sub { BinaryTree\->new( parent => $_[0] ) }, \& trigger => \e&_set_parent_for_child \& ); .Ve .PP There are three new options here, \f(CW\*(C`lazy\*(C'\fR, \f(CW\*(C`default\*(C'\fR, and \&\f(CW\*(C`trigger\*(C'\fR. The \f(CW\*(C`lazy\*(C'\fR and \f(CW\*(C`default\*(C'\fR options are linked. In fact, you cannot have a \f(CW\*(C`lazy\*(C'\fR attribute unless it has a \f(CW\*(C`default\*(C'\fR (or a \f(CW\*(C`builder\*(C'\fR, but we\*(Aqll cover that later). If you try to make an attribute lazy without a default, class creation will fail with an exception. (2) .PP In the second recipe the \fBBankAccount\fR\*(Aqs \f(CW\*(C`balance\*(C'\fR attribute had a default value of \f(CW0\fR. Given a non\-reference, Perl copies the \&\fIvalue\fR. However, given a reference, it does not do a deep clone, instead simply copying the reference. If you just specified a simple reference for a default, Perl would create it once and it would be shared by all objects with that attribute. .PP As a workaround, we use an anonymous subroutine to generate a new reference every time the default is called. .PP .Vb 1 \& has \*(Aqfoo\*(Aq => ( is => \*(Aqrw\*(Aq, default => sub { [] } ); .Ve .PP In fact, using a non\-subroutine reference as a default is illegal in Moose. .PP .Vb 2 \& # will fail \& has \*(Aqfoo\*(Aq => ( is => \*(Aqrw\*(Aq, default => [] ); .Ve .PP This will blow up, so don\*(Aqt do it. .PP You\*(Aqll notice that we use \f(CW$_[0]\fR in our default sub. When the default subroutine is executed, it is called as a method on the object. .PP In our case, we\*(Aqre making a new \f(CW\*(C`BinaryTree\*(C'\fR object in our default, with the current tree as the parent. .PP Normally, when an object is instantiated, any defaults are evaluated immediately. With our \f(CW\*(C`BinaryTree\*(C'\fR class, this would be a big problem! We\*(Aqd create the first object, which would immediately try to populate its \f(CW\*(C`left\*(C'\fR and \f(CW\*(C`right\*(C'\fR attributes, which would create a new \&\f(CW\*(C`BinaryTree\*(C'\fR, which would populate \fIits\fR \f(CW\*(C`left\*(C'\fR and \f(CW\*(C`right\*(C'\fR slots. Kaboom! .PP By making our \f(CW\*(C`left\*(C'\fR and \f(CW\*(C`right\*(C'\fR attributes \f(CW\*(C`lazy\*(C'\fR, we avoid this problem. If the attribute has a value when it is read, the default is never executed at all. .PP We still have one last bit of behavior to add. The autogenerated \&\f(CW\*(C`right\*(C'\fR and \f(CW\*(C`left\*(C'\fR accessors are not quite correct. When one of these is set, we want to make sure that we update the parent of the \&\f(CW\*(C`left\*(C'\fR or \f(CW\*(C`right\*(C'\fR attribute\*(Aqs tree. .PP We could write our own accessors, but then why use Moose at all? Instead, we use a \f(CW\*(C`trigger\*(C'\fR. A \f(CW\*(C`trigger\*(C'\fR accepts a subroutine reference, which will be called as a method whenever the attribute is set. This can happen both during object construction or later by passing a new object to the attribute\*(Aqs accessor method. However, it is not called when a value is provided by a \f(CW\*(C`default\*(C'\fR or \f(CW\*(C`builder\*(C'\fR. .PP .Vb 2 \& sub _set_parent_for_child { \& my ( $self, $child ) = @_; \& \& confess "You cannot insert a tree which already has a parent" \& if $child\->has_parent; \& \& $child\->parent($self); \& } .Ve .PP This trigger does two things. First, it ensures that the new child node does not already have a parent. This is done for the sake of simplifying the example. If we wanted to be more clever, we would remove the child from its old parent tree and add it to the new one. .PP If the child has no parent, we will add it to the current tree, and we ensure that is has the correct value for its \f(CW\*(C`parent\*(C'\fR attribute. .PP As with all the other recipes, \fBBinaryTree\fR can be used just like any other Perl 5 class. A more detailed example of its usage can be found in \fIt/recipes/basics_binarytree_attributefeatures.t\fR. .SH CONCLUSION .IX Header "CONCLUSION" This recipe introduced several of Moose\*(Aqs advanced features. We hope that this inspires you to think of other ways these features can be used to simplify your code. .SH FOOTNOTES .IX Header "FOOTNOTES" .IP (1) 4 .IX Item "(1)" Weak references are tricky things, and should be used sparingly and appropriately (such as in the case of circular refs). If you are not careful, attribute values could disappear "mysteriously" because Perl\*(Aqs reference counting garbage collector has gone and removed the item you are weak\-referencing. .Sp In short, don\*(Aqt use them unless you know what you are doing :) .IP (2) 4 .IX Item "(2)" You \fIcan\fR use the \f(CW\*(C`default\*(C'\fR option without the \f(CW\*(C`lazy\*(C'\fR option if you like, as we showed in the second recipe. .Sp Also, you can use \f(CW\*(C`builder\*(C'\fR instead of \f(CW\*(C`default\*(C'\fR. See Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild for details. .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Stevan Little .IP \(bu 4 Dave Rolsky .IP \(bu 4 Jesse Luehrs .IP \(bu 4 Shawn M Moore .IP \(bu 4 יובל קוג\*(Aqמן (Yuval Kogman) .IP \(bu 4 Karen Etheridge .IP \(bu 4 Florian Ragwitz .IP \(bu 4 Hans Dieter Pearcey .IP \(bu 4 Chris Prather .IP \(bu 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2006 by Infinity Interactive, Inc. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.