X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FGitalist.git;a=blobdiff_plain;f=local-lib5%2Fman%2Fman3%2FMoose%3A%3ACookbook%3A%3ABasics%3A%3ARecipe3.3pm;fp=local-lib5%2Fman%2Fman3%2FMoose%3A%3ACookbook%3A%3ABasics%3A%3ARecipe3.3pm;h=cba9899e07dd5d80a693f23ef2f96beba1c4fd06;hp=0000000000000000000000000000000000000000;hb=3fea05b9fbf95091f4522528b9980a33e0235603;hpb=af746827daa7a8feccee889e1d12ebc74cc9201e diff --git a/local-lib5/man/man3/Moose::Cookbook::Basics::Recipe3.3pm b/local-lib5/man/man3/Moose::Cookbook::Basics::Recipe3.3pm new file mode 100644 index 0000000..cba9899 --- /dev/null +++ b/local-lib5/man/man3/Moose::Cookbook::Basics::Recipe3.3pm @@ -0,0 +1,379 @@ +.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3 +.\" +.\" Standard preamble: +.\" ======================================================================== +.de Sh \" Subsection heading +.br +.if t .Sp +.ne 5 +.PP +\fB\\$1\fR +.PP +.. +.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 +.. +.\" Set up some character translations and predefined strings. \*(-- will +.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left +.\" double quote, and \*(R" will give a right double quote. | will give a +.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to +.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' +.\" expand to `' in nroff, nothing in troff, for use with C<>. +.tr \(*W-|\(bv\*(Tr +.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' +.ie n \{\ +. ds -- \(*W- +. ds PI pi +. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch +. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch +. ds L" "" +. ds R" "" +. ds C` "" +. ds C' "" +'br\} +.el\{\ +. ds -- \|\(em\| +. ds PI \(*p +. ds L" `` +. ds R" '' +'br\} +.\" +.\" If the F register is turned on, we'll generate index entries on stderr for +.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index +.\" entries marked with X<> in POD. Of course, you'll have to process the +.\" output yourself in some meaningful fashion. +.if \nF \{\ +. de IX +. tm Index:\\$1\t\\n%\t"\\$2" +.. +. nr % 0 +. rr F +.\} +.\" +.\" For nroff, turn off justification. Always turn off hyphenation; it makes +.\" way too many mistakes in technical documents. +.hy 0 +.if n .na +.\" +.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). +.\" Fear. Run. Save yourself. No user-serviceable parts. +. \" fudge factors for nroff and troff +.if n \{\ +. ds #H 0 +. ds #V .8m +. ds #F .3m +. ds #[ \f1 +. ds #] \fP +.\} +.if t \{\ +. ds #H ((1u-(\\\\n(.fu%2u))*.13m) +. ds #V .6m +. ds #F 0 +. ds #[ \& +. ds #] \& +.\} +. \" simple accents for nroff and troff +.if n \{\ +. ds ' \& +. ds ` \& +. ds ^ \& +. ds , \& +. ds ~ ~ +. ds / +.\} +.if t \{\ +. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" +. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' +. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' +. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' +. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' +. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' +.\} +. \" troff and (daisy-wheel) nroff accents +.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' +.ds 8 \h'\*(#H'\(*b\h'-\*(#H' +.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] +.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' +.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' +.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] +.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] +.ds ae a\h'-(\w'a'u*4/10)'e +.ds Ae A\h'-(\w'A'u*4/10)'E +. \" corrections for vroff +.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' +.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' +. \" for low resolution devices (crt and lpr) +.if \n(.H>23 .if \n(.V>19 \ +\{\ +. ds : e +. ds 8 ss +. ds o a +. ds d- d\h'-1'\(ga +. ds D- D\h'-1'\(hy +. ds th \o'bp' +. ds Th \o'LP' +. ds ae ae +. ds Ae AE +.\} +.rm #[ #] #H #V #F C +.\" ======================================================================== +.\" +.IX Title "Moose::Cookbook::Basics::Recipe3 3" +.TH Moose::Cookbook::Basics::Recipe3 3 "2009-08-11" "perl v5.8.7" "User Contributed Perl Documentation" +.SH "NAME" +Moose::Cookbook::Basics::Recipe3 \- A lazy \fBBinaryTree\fR example +.SH "SYNOPSIS" +.IX Header "SYNOPSIS" +.Vb 2 +\& package BinaryTree; +\& use Moose; +.Ve +.PP +.Vb 1 +\& has 'node' => ( is => 'rw', isa => 'Any' ); +.Ve +.PP +.Vb 6 +\& has 'parent' => ( +\& is => 'rw', +\& isa => 'BinaryTree', +\& predicate => 'has_parent', +\& weak_ref => 1, +\& ); +.Ve +.PP +.Vb 8 +\& has 'left' => ( +\& is => 'rw', +\& isa => 'BinaryTree', +\& predicate => 'has_left', +\& lazy => 1, +\& default => sub { BinaryTree\->new( parent => $_[0] ) }, +\& trigger => \e&_set_parent_for_child +\& ); +.Ve +.PP +.Vb 8 +\& has 'right' => ( +\& is => 'rw', +\& isa => 'BinaryTree', +\& predicate => 'has_right', +\& lazy => 1, +\& default => sub { BinaryTree\->new( parent => $_[0] ) }, +\& trigger => \e&_set_parent_for_child +\& ); +.Ve +.PP +.Vb 2 +\& sub _set_parent_for_child { +\& my ( $self, $child ) = @_; +.Ve +.PP +.Vb 2 +\& confess "You cannot insert a tree which already has a parent" +\& if $child\->has_parent; +.Ve +.PP +.Vb 2 +\& $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's take a look at the \f(CW\*(C`node\*(C'\fR attribute: +.PP +.Vb 1 +\& has 'node' => ( is => 'rw', isa => 'Any' ); +.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's move on to the \f(CW\*(C`parent\*(C'\fR attribute: +.PP +.Vb 6 +\& has 'parent' => ( +\& is => 'rw', +\& isa => 'BinaryTree', +\& predicate => 'has_parent', +\& 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 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'll just look at +\&\f(CW\*(C`left\*(C'\fR: +.PP +.Vb 8 +\& has 'left' => ( +\& is => 'rw', +\& isa => 'BinaryTree', +\& predicate => 'has_left', +\& 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 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'll 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's \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 simply +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 'foo' => ( is => 'rw', default => sub { [] } ); +.Ve +.PP +In fact, using a non-subroutine reference as a default is illegal in Moose. +.PP +.Vb 2 +\& # will fail +\& has 'foo' => ( is => 'rw', default => [] ); +.Ve +.PP +This will blow up, so don't do it. +.PP +You'll 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're 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'd 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's 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's 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 ) = @_; +.Ve +.PP +.Vb 2 +\& confess "You cannot insert a tree which already has a parent" +\& if $child\->has_parent; +.Ve +.PP +.Vb 2 +\& $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/000_recipes/moose_cookbook_basics_recipe3.t\fR. +.SH "CONCLUSION" +.IX Header "CONCLUSION" +This recipe introduced several of Moose's 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 \*(L"mysteriously\*(R" because +Perl's reference counting garbage collector has gone and removed the +item you are weak\-referencing. +.Sp +In short, don't 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::Recipe8 for details. +.SH "AUTHORS" +.IX Header "AUTHORS" +Stevan Little +.PP +Dave Rolsky +.SH "COPYRIGHT AND LICENSE" +.IX Header "COPYRIGHT AND LICENSE" +Copyright 2006\-2009 by Infinity Interactive, Inc. +.PP + +.PP +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself.