in reply to Multiple Prototypes for Object Constructor

I would like to suggest my way to solve this problem. In my mind, it's better to use _init() method for initialization your object if it needs:
package Edge; use vars qw(%FIELDS); # Define class properties use fields qw(from_node to_node label); { # Default values for class properties my $_def_values = { from_node => undef, to_node => undef, label => undef, }; sub _class_def { $_def_values; } } # Constractor sub new { my $proto = shift; my $class = ref($proto) || $proto; # Define default values for object instance my $defaults = ref($proto) ? $proto : $class->_class_def(); my $self = {}; for(keys %$defaults) { $self->{$_} = $defaults->{$_} } + bless $self, $class; $self->_init(@_) if @_; return $self; } # Initialization of object sub _init { my $self = shift; my %params = @_; for my $par(keys %params) { $self->{$par} = $params{$par} if exists $FIELDS{$par}; } }
      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

Replies are listed 'Best First'.
•Re: Re: Multiple Prototypes for Object Constructor
by merlyn (Sage) on May 22, 2003 at 18:20 UTC
      Thanks for correction. Maybe you are right. My experiense is not enough for confirmation or refutation your assertion. I use a book 'Object Oriented Perl' by Damian Conway and there are two ways to do a clon of existing object (p. 104). In my mind, for person, who use some class, it's easier to call one method new() for creation or copy of an object intead of calling separate methods for this.
            
      --------------------------------
      SV* sv_bless(SV* sv, HV* stash);