Wiggins d Anconia wrote:

>
> my $new_obj = ref($obj)->new;

Ouch!  Unfortunately, I can well imagine someone trying to use that, too.

> I do have to differ with the opinion about new, if you really are just
> constructing an object then to me it still makes sense as a name, but in
> the case of DBI you are constructing an object *and* performing an
> action, so the 'connect' name makes sense. To me it would also make
> sense if there was (and maybe there is):
>
> my $dbi = new DBI;
> my $dbh = $dbi->connect;
>
> Because in this case you are *just* constructing an object, and then
> *just* connecting...  But this assumes you throw out the history of the
> corruption of the name 'new' both as a cloning method and as "let's do
> more than just create an object, lets also connect to a server, read a
> file, etc." which somehow I don't think Randal will let us ;-) (and he
> probably is right not to)....

I agree [I think ;-)]  For creating a new object, the only way I would use it,
new makes sense.

Even for cloning, it can be used without the kind of confusion that Randal
refers to:
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 = $_[0];
      $self->{$_} = $source->{$_} for keys %$source;
   }else {
      $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;
^Z
MyObject=HASH(0x15d55ec)
Yes
first: 1
second: 2
MyObject=HASH(0x1a876d4)
Yes
first: 1
second: 2



Joseph


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

Reply via email to