For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > How to overload the constructor?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to overload the constructor?
howachen@gmail.com

2006-07-30, 9:57 pm

in the base class:
------------------------------
sub new {

my ( $class, $var1) = @_;

my $self = {
var1=> undef,
};

$self->{var1} = $dbh if defined($var1);

bless $self, $class;

return $self;
}
------------------------------

in the child class:
------------------------------
sub new {....}
------------------------------

but since we don't have the class created yet (class is created at
bless...which is in the parent class)

So: is it possilbe to re-use the parent without putting the bless....
in the child class?

Paul Lalli

2006-07-31, 7:09 pm

howachen@gmail.com wrote:
> in the base class:
> ------------------------------
> sub new {
> my ( $class, $var1) = @_;
> my $self = {
> var1=> undef,
> };
> $self->{var1} = $dbh if defined($var1);
> bless $self, $class;
> return $self;
> }
> ------------------------------
>
> in the child class:
> ------------------------------
> sub new {....}
> ------------------------------
>
> but since we don't have the class created yet (class is created at
> bless...which is in the parent class)


No, the class is created when Perl reads in the entire module's source
code. The reference becomes an object of that class at the bless
statement.

Your sentence is also very incomplete. "since <foo>....", what?

> So: is it possilbe to re-use the parent


"re-use the parent" is a nonsensical term. "parent" is not "used", and
so cannot be "re-used". What are you actually trying to do? If
English is not your first language, try speaking Perl instead of
English. If English is your first language, try using complete
sentences, with terms that actually make sense in a programming
environment.

> without putting the bless.... in the child class?


This, again, makes no sense. If the child's constructor has no
"bless", then the child's constructor isn't creating an object of the
child class. Once more, what are you actually *trying* to do?

Also, none of anything in your post has anything to do with your
subject, as you are not "overloading" anything.

Are you, perhaps, under the impression that a Parent's constructor is
automatically called when a child's constructor is called? That may be
true in some languages, but not in Perl.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com