sman has asked for the wisdom of the Perl Monks concerning the following question:

When I read the DESCRITION section of Moose::Object (1), I agree with what it says.
(1) "This class is the default base class for all Moose-using classes. Whe +n you use Moose in this class, your class will inherit from this clas +s."
However, When I check the source code(2) of Moose.pm, It seems to me that Moose.pm only use Moose::Object, that is , import its functions, instead of inheriting from it.
(2) package Moose; .... use Moose::Object;
So, do I miss something here?

Replies are listed 'Best First'.
Re: Moose & Moose::Object
by stvn (Monsignor) on Jan 24, 2010 at 02:01 UTC
    However, When I check the source code(2) of Moose.pm, It seems to me that Moose.pm only use Moose::Object, that is , import its functions, instead of inheriting from it.

    Actually Moose.pm is using Moose::Object, but it is not importing it's functions. In order for that to happen Moose::Object would have to define an import method as well as implement code to do the importing, neither of which you will see if you read the source.

    Instead what happens is that Moose.pm defines an import method when it calls Moose::Exporter->setup_import_methods (about line 121 of Moose 0.94), that import method eventually calls the init_meta method defined right below that. In init_meta the "base_class" is assigned as 'Moose::Object' (assuming you didn't tell Moose to do otherwise) and eventually that will become the value of @ISA in your package. If you later call extends 'DBIx::Class' in your code, then the value of @ISA will then be DBIx::Class and no longer be Moose::Object.

    The idea here is that Moose wants to provide you with the basic mechanisms needed by just about every object, such as: constructor (new), initialization mechanism (BUILDALL/BUILD), destructor (DESTROY), a teardown mechanism (DEMOLISHALL/DEMOLISH) and some introspection methods (does, DOES, meta, etc). In order to do this it makes the most sense to inherit from Moose::Object as a base class.

    So, do I miss something here?

    No, I think your just thinking about it too hard, and perhaps not asking the right questions. Based on your last few posts I suspect you are trying to mix Moose and DBIx::Class in some way. This has actually been done many times before to varying degrees of success, for some definition of success. The real solution is to re-write DBIx::Class in Moose and as far as I know there are some plans for doing just that (not sure how far along they are or aren't at this point).

    If you want to understand the guts of Moose then I recommend first reading Class::MOP as it is simpler to digest and will give you the foundation with which to understand some of the more complex aspects of Moose. Perhaps you might also consider visiting us on IRC in #moose or #moose-dev, we will be happy to answer your detailed questions there. Myself (the original author) and the entire core development team are pretty much always in channel.

    -stvn
      Awesome. Thanks a lot.
Re: Moose & Moose::Object
by ikegami (Patriarch) on Jan 23, 2010 at 23:09 UTC

    It seems to me that Moose.pm only use Moose::Object, that is , import its functions

    How Moose uses Moose::Object is of no consequence to whether your class inherits from Moose::Object or not. It's not like your class is inheriting from Moose.

Re: Moose & Moose::Object
by Anonymous Monk on Jan 23, 2010 at 23:00 UTC
    However, When I check the source code(2) of Moose.pm, It seems to me that Moose.pm only use Moose::Object, that is , import its functions, instead of inheriting from it.

    That seems to be the case as this code demonstrates

    $ perl -le" package Yo; use Moose; print Yo->new->isa('Moose')" $ perl -le" package Yo; use Moose; print Yo->new->isa('Moose::Object') +" 1 $ perl -le" use Moose; print Moose::Object->new; print Moose->new; " Moose::Object=HASH(0xf86fa4) Can't locate object method "new" via package "Moose" at -e line 1. $
    Yo is a Moose-using class, and it inherits from Moose::Object, not Moose.

    So, do I miss something here?

    You're thinking too hard about how it works, purpose of Moose is to make your life easy, just use it :)