in reply to Calling SUPER::new from classes built with Class::MethodMaker?

I'd go with the init() function. One can call the superclass' $self->SUPER::init(@_) from that.

Just my 2 cents, -gjb-

For an example, see below:

Parent class:


package Parent; use strict; use warnings; use Class::MethodMaker new_hash_init => 'new', get_set => [qw( color )]; sub init { my $self = shift(); print STDERR "initializing parent\n"; my %values = @_; $self->color($values{color}); } 1;

Child class:


package Child; use strict; use warnings; use Class::MethodMaker new_hash_init => 'new', get_set => [qw( shape )]; use base qw( Parent ); sub init { my $self = shift(); my %values = @_; $self->SUPER::init(color => ($values{color})); $self->shape($values{shape} || 'square'); } 1;

Main program:


use strict; use warnings; use Parent; use Child; my $p = new Parent(color => 'green'); my $c = new Child(shape => 'round', color => 'red'); print $p->color(), "\n"; print $c->color(), ", ", $c->shape(), "\n";

Replies are listed 'Best First'.
Re: Re: Calling SUPER::new from classes built with Class::MethodMaker?
by Anonymous Monk on Feb 12, 2003 at 01:34 UTC
    Interesting, thanks. Unfortunately, I've got some modules from CPAN which do important work in new() rather than init, like Exception::Class from CPAN. Calling SUPER::init misses all the secret sauce happening in the super class constructor. for the moment I'm bypassing Class::MethodMaker in those classes which need to call SUPER::new but it seem like a bit of a hack. It would be great to have an option to Class::MethodMaker to call SUPER::new in the generated constructor....

      (Commenting on a very old thread)

      I agree, it would be nice if MehodMaker were extended so that it could call SUPER::new().

      I found this thread because I'm trying to figure out how to get MethodMaker-derived classes to call *any* sort of SUPER::__() function. Right now I'm bugging the perl-beginners mailing list about why it doesn't work.

      -Dan