Skip to content

Commit

Permalink
ref ($proto) || $proto patch
Browse files Browse the repository at this point in the history
Message-ID: <406F7A0A.50702@yahoo.com>
(with minor reformatting)

p4raw-id: //depot/perl@22654
  • Loading branch information
Ovid authored and rgs committed Apr 5, 2004
1 parent 46ec2f1 commit eac7fe8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
13 changes: 7 additions & 6 deletions pod/perlobj.pod
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 11 additions & 35 deletions pod/perltoot.pod
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit eac7fe8

Please sign in to comment.