Re: Calling SUPER::constructor in the constructor

2004-02-01 Thread James Edward Gray II
On Feb 1, 2004, at 1:10 AM, R. Joseph Newton wrote:

To answer your immediate
question, there is a way to call functions of the parent, but with 
another
step in indeirection:
$child_class->SUPER->new.

You should rarely need this though.  Any attributes [ie hash members] 
of the
parent class that are relevant in your child object can simply be 
addressed
thorugh the constructor of the child class.
and

Well, that worked, but note that the following constructor for the 
MyChild
object worked just as well:

sub new {
  my $class = shift;
  my $name = shift;
  my $age = shift;
  my $self = {};
  bless $self, $class;
  $self->{name} = $name;
  $self->{age} =  $age;
  return $self;
}
and the name and age attributes set through the child constructor were 
just
as available to the bemoan_age accessor provided by the base class.  
I'm not
sure I see any benefit to calling SUPER:: methods except in cases 
where a
parent method is overshadowed but still needed.
I think SUPER is a little more important than you give it credit for.  
I would say it's very common to use in a child's constructor.

I find your suggestion of duplicating the parent's construction scary 
OO thinking and I hope we're assuming you wrote both objects, at the 
very least.  Still, one day you're going to change one and forget the 
other...  Aren't we always telling people not to "reinvent the wheel" 
and to "use modules"?  It's really the same issue, I think.  I would 
much rather see people using SUPER than rewriting code.

SUPER is also quite handy when you're overriding a method, but just 
want to extend its functionality.  Make the call then do the rest.  
Simple, flexible, powerful.

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Calling SUPER::constructor in the constructor

2004-02-01 Thread R. Joseph Newton
Dan Anderson wrote:

> Is it possible to call the constructor that a function inherits from its
> parent?  I tried calling SUPER:: and SUPER-> in a constructor and got
> errors.  Am i correct in assuming that if I rewrite the constructor that
> a copy of the parent object won't be available?

If you have a copy of the derived object, you have a copy of the parent
object.  The relationship is an "izza", not a 'hazza'.  No containment is
going on in an OO inheritance relationship.  To answer your immediate
question, there is a way to call functions of the parent, but with another
step in indeirection:
$child_class->SUPER->new.

You should rarely need this though.  Any attributes [ie hash members] of the
parent class that are relevant in your child object can simply be addressed
thorugh the constructor of the child class.  Any instance methods are
automatically available.  If they have not been overshadowed by redfinition
in the derived package, they will be available just as defined in the root
class.

package MyParent;

use Exporter;

our @ISA = 'Exporter';
our @EXPORT = qw /all/;

sub new {
  my $class = shift;
  my $self = {};
  bless $self, $class;
  $self->{name} = $_[0];
  $self->{age} = $_[1];
  return $self;
}

sub bemoan_age {
  my $self = shift;
  print "Oh what a drag it is getting old.  I can't believe $self->{name}" .

  " has been around $self->{age} years\n";
}

package MyChild;

use MyParent;

our @ISA = ('MyParent');

sub new {
  my $class = shift;
  my $name = shift;
  my $age = shift;
  my $self = $class->SUPER::new($name, $age);
  return $self;
}

sub print_stats {
  my $self = shift;
  print "Name: $self->{name}\n";
  print "Age:  $self->{age}\n";
}

Greetings! E:\d_drive\perlStuff>perl -w -MMyChild
my $kid = MyChild->new('Joseph', [huh?]);
$kid->print_stats;# derived class method
$kid->bemoan_age;#  parent class method
^Z
Name: Joseph
Age:  [huh?]
Oh what a drag it is getting old.  I can't believe Joseph has been around
[huh?] years

Well, that worked, but note that the following constructor for the MyChild
object worked just as well:


sub new {
  my $class = shift;
  my $name = shift;
  my $age = shift;
  my $self = {};
  bless $self, $class;
  $self->{name} = $name;
  $self->{age} =  $age;
  return $self;
}

and the name and age attributes set through the child constructor were just
as available to the bemoan_age accessor provided by the base class.  I'm not
sure I see any benefit to calling SUPER:: methods except in cases where a
parent method is overshadowed but still needed.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Calling SUPER::constructor in the constructor

2004-01-22 Thread drieux


On Jan 22, 2004, at 9:13 AM, Dan Anderson wrote:

Is it possible to call the constructor that a function inherits from 
its
parent?  I tried calling SUPER:: and SUPER-> in a constructor and got
errors.  Am i correct in assuming that if I rewrite the constructor 
that
a copy of the parent object won't be available?
[..]

did you try something like

sub new
{
my $type  = shift;
# call our parent class new to get our $self
my $self = $type->SUPER::new();
.
}
cf:



ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Calling SUPER::constructor in the constructor

2004-01-22 Thread James Edward Gray II
On Jan 22, 2004, at 11:13 AM, Dan Anderson wrote:

Is it possible to call the constructor that a function inherits from 
its
parent?
Yes.

I tried calling SUPER:: and SUPER-> in a constructor and got
errors.
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
# ...
}
Am i correct in assuming that if I rewrite the constructor that
a copy of the parent object won't be available?
Yes.

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: Calling SUPER::constructor in the constructor

2004-01-22 Thread Dan Muey
> Is it possible to call the constructor that a function 
> inherits from its parent?  I tried calling SUPER:: and 
> SUPER-> in a constructor and got errors.  Am i correct in 
> assuming that if I rewrite the constructor that a copy of the 
> parent object won't be available?
> 

Perhaps some example of your code. Also I can do DBI->connect(..) or DBI::connect and 
it works.
Why not look at that and see how DBI does it?

Sorry it's not more helpful, but without more details

HTH

DMuey

> Thanks in advance,
> 
> Dan
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED] 
 



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Calling SUPER::constructor in the constructor

2004-01-22 Thread Dan Anderson
Is it possible to call the constructor that a function inherits from its
parent?  I tried calling SUPER:: and SUPER-> in a constructor and got
errors.  Am i correct in assuming that if I rewrite the constructor that
a copy of the parent object won't be available?

Thanks in advance,

Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]