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

for example although XML::Twig inherits from XML::Parser it explicitly instantiates a Parser object inside its constructor, but it looks in Perl that is explicit and optional. I m confused about if I can use a base class instance in place of a derived instance polymorphic wise? Are base and child classes connected polymorphicaly?

Replies are listed 'Best First'.
Re^3: OOP and derived classes
by LanX (Saint) on Feb 14, 2015 at 16:55 UTC
    I think you are referring to these lines from XML::Twig (taken from here)

    sub new { my ($class, %args) = @_; my $handlers; # change all nice_perlish_names into nicePerlishNames %args= _normalize_args( %args); # check options unless( $args{MoreOptions}) { foreach my $arg (keys %args) { carp "invalid option $arg" unless $valid_option{$arg}; } } # a twig is really an XML::Parser # my $self= XML::Parser->new(%args); my $self; $self= XML::Parser->new(%args); bless $self, $class; #... yadda yadda

    and indeed it does inherit from XML::Parser @ISA = qw(XML::Parser);

    This seems to be a highly complex module also playing with UNIVERSAL so I don't feel competent to comment.

    I'd say the new() method is inherited but overridden, and instead of using SUPER::new() an explicit XML::Parser->new() is used to initialize a parent object and alter it.

    TIMTOWTDI !

    Never used SUPER or Java so take it with a grain of salt, others with more expertise will comment soon on the basis of this post.

    update

    I hope it's obvious that analyzing XML::Twig is not the best way to start learning Perl's OOP. :)

    Cheers Rolf

    PS: Je suis Charlie!

      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?

        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.

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