Terrence,

On Dec 22, 2008, at 12:02 AM, Terrence 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?

Actually, clear() is nothing special, it is just a user-defined method and it is not part of the MOP.

What if the class and it's superclass define clear without the
subclass using 'override'

Same thing that would normally happen in vanilla Perl OO.

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.

This is just a style thing, I like the way the quotes make the method stand out, you can omit them if you like.

Basic::2
--------
Is clear() called even if no clear() method is defined? What about BUILD?

Again, clear() is nothing special, so it is not really relevant here.

The BUILD methods are called in order for the entire inheritance hierarchy, which means that if no BUILD method is defined, nothing gets called.

Comment: the parallel bars form of 'or' should not be used here ... 'or' should
also && should not be used ... 'and' should

This is a style thing, that is just how I write it TIMTOWTDI :)

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?
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.

Yup, exactly.

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, it always gets a hashref.

- what is the entire sequence of things that happen when ->new() is
called?

The source of Moose::Object::new is actually pretty simple:

First it calls BUILDARGS on the class, this is used to canonicalize the arguments and can be overridden to allow different arrangements of arguments to be accepted by new().

Next it calls ->meta->new_object, which does all the meta-fiddling to construct the object instance.

Finally it runs BUILDALL, which traverses the inheritance hierarchy, finds all the BUILD methods defined and calls them in order.

- 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.

Nope, again, clear() is not in the MOP. Your hooks are BUILDARGS and BUILD, in general you don't want to override new() and you really shouldn't ever need to touch BUILDALL.

- Moose::Util::TypeConstraints is not needed in package Company is it?

Nope, it doesn't seem to, probably did at one point and should be removed now.

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?

It checks them against the specified type constraints until it finds a match, then applies the associated coercion.

- 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?

A blessed reference, same as regular Perl 5. The Moose types are meant to model the Perl 5 types and extend them, but core types like Object will always remain compat with vanilla Perl 5.

- 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 and No. Yes, that code is basically what is being done by coerce. No, because the default sub will only fire if no value has been provided for that argument.

Hope this helps,

- Stevan









Reply via email to