Re: OOP and derived classes
by LanX (Saint) on Feb 14, 2015 at 15:51 UTC
|
I think you should post some example code, maybe taken from the PODs.
Since you are using Java terminology, I'm not really sure what you mean.
Probably you are asking about perlobj#SUPER ?
edit
There is a variety of alternative "modern" OO models available, starting from "Mo" till "Moose", unfortunately I'm too confused to suggest which one to chose.
But they are heavily documented and "tutorialed"! =)
update
hmm coming from Java you will most probably like Moose, see perl moose tutorial
| [reply] |
|
|
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?
| [reply] |
|
|
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. :)
| [reply] [d/l] [select] |
|
|
|
|
|
|
Re: OOP and derived classes
by choroba (Cardinal) on Feb 14, 2015 at 16:07 UTC
|
Are you talking about the newInstance method? Such a thing is generally impossible in Perl, as you don't know what method might be a constructor. New instance of a class is created by calling the constructor, or by any other documented way (calling a factory etc.).
For a more advanced stuff, see Class::MOP::Class.
| [reply] |
Re: OOP and derived classes
by RichardK (Parson) on Feb 14, 2015 at 16:04 UTC
|
Perhaps reading perlootut will help, it explains how perl OO handles inheritance and encapsulation.
| [reply] |
Re: OOP and derived classes
by Anonymous Monk on Feb 14, 2015 at 19:31 UTC
|
In Perl when you create a new derived class should you explicitly create a base instance inside the derived class constructor?
Here's a simple answer: yes, you should. You don't get 'base instance' automatically.
Longer version: I don't know Java, but I suppose Java's object is (a pointer to) a struct-like thing, and a derived class appends its part (instance variables or whatever Java calls them) to the 'base part'. OTOH, Perl's object is (a reference to) anything at all. For example, a string, a number, array, hash, function... There is no 'base instance' and 'derived instance', just 'instance'. Here's a function object:
$ perl -E 'sub method { my $self = shift; $self->() }
my $obj = sub { say "HELLO" };
bless $obj;
$obj->method()'
HELLO
| [reply] [d/l] |
|
|
>Here's a simple answer: yes, you should. You don't get 'base >instance' automatically.
but it's looks like its optional in Perl? what happens when the base class is not instantiated and I call a base class method which accesses some base class properties ,through a derived object ? will the method be called as a base class method or will it be called as a derived one (the first parameter sent to the method would be a base object or a derived one?
| [reply] |
|
|
but it's looks like its optional in Perl?
Yes.
what happens when the base class is not instantiated and I call a base class method which accesses some base class properties ,through a derived object ?
If, say, the object in question is a hash, then base class will find undef instead of whatever it wanted to find, like with all other hashes.
will the method be called as a base class method or will it be called as a derived one (the first parameter sent to the method would be a base object or a derived one?
The base class will get the whole thing, 100% of it. So, in Java's terms, the base method will get the derived object.
| [reply] [d/l] |
|
|
|
|
|
|
|
|
|