Sorry, this was clumsy and graceless:

"R. Joseph Newton" wrote:


> sub new {
> ...

>   }else {
>       $self->initialize(@_);
>    }
>    return $self;
> }
>

It works much better without the else here.  Inializing with the remainder of the
parameter list allows ad-hoc expansion of the object attributes.  So:

package MyObject;

use strict;
use warnings;

use Exporter;

sub new {
   my $class = shift;   # point taken

   my $self = {};
   bless $self, $class;
   if ($_[0] && $_[0]->isa($class) ) {
      my $source = shift;
      $self->{$_} = $source->{$_} for keys %$source;
   }

   $self->initialize(@_);
   return $self;
}

sub initialize {
  my $self = shift;
  my %source = @_;
  $self->{$_} = $source{$_} foreach keys %source;
}
1;

Greetings! E:\d_drive\perlStuff>perl -w -MMyObject
my $obj = MyObject->new qw(first 1 second 2);
print $obj, "\n";
print "Yes\n" if $obj->isa(MyObject);
print "$_: $obj->{$_}\n" for keys %$obj;
my $obj2 = MyObject->new($obj);
print $obj2, "\n";
print "Yes\n" if $obj2->isa(MyObject);
print "$_: $obj2->{$_}\n" for keys %$obj2;
my $obj3 = MyObject->new($obj, qw(third 3 fourth 4));
print $obj3, "\n";
print "Yes\n" if $obj3->isa(MyObject);
print "$_: $obj3->{$_}\n" for keys %$obj3;
^Z
MyObject=HASH(0x15d55ec)
Yes
first: 1
second: 2
MyObject=HASH(0x1a87824)
Yes
first: 1
second: 2
MyObject=HASH(0x1a45afc)
Yes
first: 1
second: 2
third: 3
fourth: 4

Greetings! E:\d_drive\perlStuff>


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

Reply via email to