From: Ovid Date: Sat, 3 Apr 2004 18:59:22 +0000 (-0800) Subject: ref ($proto) || $proto patch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eac7fe86f47901babc387c242e8dc65d73838746;p=p5sagit%2Fp5-mst-13.2.git ref ($proto) || $proto patch Message-ID: <406F7A0A.50702@yahoo.com> (with minor reformatting) p4raw-id: //depot/perl@22654 --- diff --git a/pod/perlobj.pod b/pod/perlobj.pod index 7d7beaf..891ebe3 100644 --- a/pod/perlobj.pod +++ b/pod/perlobj.pod @@ -97,9 +97,11 @@ so that your constructors may be inherited: } Or if you expect people to call not just C<< CLASS->new() >> but also -C<< $obj->new() >>, then use something like this. The initialize() -method used will be of whatever $class we blessed the -object into: +C<< $obj->new() >>, then use something like the following. (Note that using +this to call new() on an instance does not automatically perform any +copying. If you want a shallow or deep copy of an object, you'll have to +specifically allow for that.) The initialize() method used will be of +whatever $class we blessed the object into: sub new { my $this = shift; @@ -485,9 +487,8 @@ if you don't care to leak. For example, here's a self-referential node such as one might use in a sophisticated tree structure: sub new_node { - my $self = shift; - my $class = ref($self) || $self; - my $node = {}; + my $class = shift; + my $node = {}; $node->{LEFT} = $node->{RIGHT} = $node; $node->{DATA} = [ @_ ]; return bless $node => $class; diff --git a/pod/perltoot.pod b/pod/perltoot.pod index 8d224dd..4a212fb 100644 --- a/pod/perltoot.pod +++ b/pod/perltoot.pod @@ -224,23 +224,8 @@ The second argument is the class into which the referent will be blessed. By not assuming our own class as the default second argument and instead using the class passed into us, we make our constructor inheritable. -While we're at it, let's make our constructor a bit more flexible. -Rather than being uniquely a class method, we'll set it up so that -it can be called as either a class method I an object -method. That way you can say: - - $me = Person->new(); - $him = $me->new(); - -To do this, all we have to do is check whether what was passed in -was a reference or not. If so, we were invoked as an object method, -and we need to extract the package (class) using the ref() function. -If not, we just use the string passed in as the package name -for blessing our referent. - sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = {}; $self->{NAME} = undef; $self->{AGE} = undef; @@ -401,8 +386,7 @@ it instead a file-scoped lexical, you should make these changes to your Person::new() constructor: sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = {}; $Census++; $self->{NAME} = undef; @@ -458,8 +442,7 @@ a public data member in the same way that NAME, AGE, and PEERS are. to perl version 5.004 we'll have to quote the field name.) sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = {}; $self->{NAME} = undef; $self->{AGE} = undef; @@ -650,8 +633,7 @@ Ok. To do this, we'll change Person::new() so that it supports a full name field this way: sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = {}; $self->{FULLNAME} = Fullname->new(); $self->{AGE} = undef; @@ -683,8 +665,7 @@ by the appropriate name to access them: use strict; sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = { TITLE => undef, CHRISTIAN => undef, @@ -1009,8 +990,7 @@ know about its immediate superclass, but never vice-versa.) So let's fix up Employee::new() this way: sub new { - my $proto = shift; - my $class = ref($proto) || $proto; + my $class = shift; my $self = $class->SUPER::new(); $self->{SALARY} = undef; $self->{ID} = undef; @@ -1244,8 +1224,7 @@ different: package Person; sub new { - my $that = shift; - my $class = ref($that) || $that; + my $class = shift; my $self = { NAME => undef, AGE => undef, @@ -1376,8 +1355,7 @@ constructor will look like when taking this approach: ); sub new { - my $that = shift; - my $class = ref($that) || $that; + my $class = shift; my $self = { _permitted => \%fields, %fields, @@ -1445,9 +1423,8 @@ Here's how to be careful: ); sub new { - my $that = shift; - my $class = ref($that) || $that; - my $self = bless $that->SUPER::new(), $class; + my $class = shift; + my $self = $class->SUPER::new(); my($element); foreach $element (keys %fields) { $self->{_permitted}->{$element} = $fields{$element}; @@ -1655,8 +1632,7 @@ update value fields in the hash. Convenient, eh? # this is the same as before... sub new { - my $that = shift; - my $class = ref($that) || $that; + my $class = shift; my $self = { NAME => undef, AGE => undef,