diff options
author | Ovid <publiustemp-p5p@yahoo.com> | 2004-04-03 10:59:22 -0800 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-04-05 15:24:49 +0000 |
commit | eac7fe86f47901babc387c242e8dc65d73838746 (patch) | |
tree | 7c25ef6bdd95bad7571d4b38f79eee6b90ac56d1 /pod/perltoot.pod | |
parent | 46ec2f140c7050c2c6553ebf8e07a3f368560ff0 (diff) | |
download | perl-eac7fe86f47901babc387c242e8dc65d73838746.tar.gz |
ref ($proto) || $proto patch
Message-ID: <406F7A0A.50702@yahoo.com>
(with minor reformatting)
p4raw-id: //depot/perl@22654
Diffstat (limited to 'pod/perltoot.pod')
-rw-r--r-- | pod/perltoot.pod | 46 |
1 files changed, 11 insertions, 35 deletions
diff --git a/pod/perltoot.pod b/pod/perltoot.pod index 8d224dd341..4a212fba91 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<or> 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, |