On Mon, Dec 22, 2008 at 12:02 AM, Terrence <scheme...@gmail.com> wrote: > After reading and working through the Moose Cookbook, I had the following > questions after said examples. I would appreciate answers to these > questions. > > Basic::1 > -------- > When is clear() called in the object construction sequence? What is > the purpose of clear()? I presume to reset an object to default values > at anytime, including object construction?
First clear() as a method doesn't exist. You can define a clearer method with whatever name you wish by passing clearer => 'method_name'. 'method_name' can be 'clear'. A clearer method isn't called during object construction. > What if the class and it's superclass define clear without the > subclass using 'override' Same thing as any method in Perl. 'override' just enables the 'super' keyword. > Why is the fat comma used and yet single quotes around the word 'clear' > like so > > after 'clear' => sub { > > ... I personally sort of like the quotes because it makes the sub-name > stand out more. It's a matter of the style of the author of that section (namely stevan). > > Basic::2 > -------- > Is clear() called even if no clear() method is defined? What about BUILD? As above, a clearer method is only ever called manually, and not during object construction. BUILD methods are called on every class in the inheritance tree where defined. This is handled via the BUILDALL method in Moose::Object and uses methods in the MetaObject Protocol (MOP) to provide the feature. > Comment: the parallel bars form of 'or' should not be used here ... 'or' > should > also && should not be used ... 'and' should > > > Basic::3 > -------- > > how can I rewrite: > > default => sub { BinaryTree->new(parent => $_[0]) }, > > so that it refers to a named sub? Is there more than one way to do so? either: default => sub { shift->named_method(@_) } or the newer form of builder => 'named_method' > This is more of a basic Perl question, but there is no such thing as a > dumb question! > > I'm thinking default => \&sub_name, > will do the trick. that would work as well but 'builder => method_name' is cleaner > > Basic::4 > -------- > > In order to understand BUILD better: > - does it always receive a hashref of what ->new() was called with > regardless of whether it was called with a hash or hashref? Yes. > - what is the entire sequence of things that happen when ->new() is > called? Look inside Moose::Object where new() is defined. > - in terms of public API hooks and customization, what happens when > ->new() is called? The two public hooks appear to be clear() and > BUILD() but I can find no docs on exactly when and how each is called. As I've said above, clear() doesn't exist, and new() is defined in Moose::Object. > - Moose::Util::TypeConstraints is not needed in package Company is it? Doesn't look like it. > Basic::5 > -------- > > - the coerce keyword can coerce from a number of things. If it is > coercing from Object, how does it know that $_ is an Object? If it > is to coerce from Str, how does it know that $_ is a Str? To check if an Object is an Object it uses Scalar::Util's blessed(), and a Str is a Str if it isn't a ref, and doesn't look like a number (using I believe Scalar::Utils looks_like_number). > - After looking through Moose::Util::TypeConstraints, I'm wondering > what constitutes an object. Must it be a Moose object? Old-style > Perl 5 object? Class::Prototyped object? All of the above will work, it must be blessed. > - a non-declarative, but equivalent way to coerce arrayrefs and > hashrefs wouldve been with a default sub like this, correct? > > sub { > > my $r = ref $_[0]; > > if ($r eq 'ARRAY') { > HTTP::Headers->new( @{ $_ } ) ; > } elsif ($r eq 'HASH) { > HTTP::Headers->new( %{ $_ } ) ; > } else { > HTTP::Headers->new; > } > > } Yes, but that isn't very extensible as every time you wanted to add a new coercion you'd have to change the implementation of the default subroutine. Also coercions are global by default, so anywhere you have a Type and a coercion defined the coercion function can be triggered. -Chris