in reply to Re^3: OOP and derived classes
in thread OOP and derived classes

but do the other more simple modules follow this model of instantiating the base class, inside their constructor? Is it ok if I don't learn classic Perl OOP and just straight into Moose? What is the disadvantage of that?

Replies are listed 'Best First'.
Re^5: OOP and derived classes
by Corion (Patriarch) on Feb 14, 2015 at 19:14 UTC

    The main disadvantage of not knowing classic Perl OOP is that most of the existing code does not use Moose.

    I feel that Moose is massive overkill for having mostly just a (somewhat) nice syntax for declaring your class properties. You also get class introspection where you can introspect the inheritance tree and (Moose-declared) fields of your classes through a defined API. Personally, I haven't seen any tooling that actively exploits this feature, like some enhanced debugger that lets you conveniently watch (Moose) objects, or a webpage that automatically generates forms for Moose objects, but that might be because I haven't looked closely enough or filtered them out by suspecting too much magic at play.

    I think that tye's argument against Moose is that it leads to classes being little more than glorified hashes by making the bad things easy (autogenerating accessors) while bringing no support for the good things of Class/Object design, namely designing an API to interact with the object beyond getting/setting properties on the object.

    So, what I'm trying to say, from my point of view, only knowing Moose will possibly prevent you from using/implementing a lighter-weight object system. It certainly won't prevent you from writing OOP-style code.

Re^5: OOP and derived classes
by LanX (Saint) on Feb 14, 2015 at 19:48 UTC
    > but do the other more simple modules follow this model of instantiating the base class, inside their constructor?

    yes!

    An object $obj in Perl is just a reference (to a data container, normally hashes) combined with a pointer to a package (read class). This coupling is done with bless and is also dynamic.

    Any method-call on an object like $obj->meth(args...) looks for a sub called meth in the blessed package or otherwise in its inheritance chain (see @ISA )

    When meth() is found it gets called with meth($obj,args...) and by convention the first parameter has to be stored into a variable called $self .

    Any method can be a constructor (i.e. putting data into a structure and returning the blessed reference), calling it by Class->new() is just another convention (in this case the first argument will be the string "Class" normally put into something like $pckg ). You can even have multiple constructors.

    That's basically all you need to know!

    It seems very raw and rudimentary at the beginning but after understanding it you can model most other object models.

    Cheers Rolf

    PS: Je suis Charlie!