}
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;
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;
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;
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;
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;
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;
use strict;
sub new {
- my $proto = shift;
- my $class = ref($proto) || $proto;
+ my $class = shift;
my $self = {
TITLE => undef,
CHRISTIAN => undef,
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;
package Person;
sub new {
- my $that = shift;
- my $class = ref($that) || $that;
+ my $class = shift;
my $self = {
NAME => undef,
AGE => undef,
);
sub new {
- my $that = shift;
- my $class = ref($that) || $that;
+ my $class = shift;
my $self = {
_permitted => \%fields,
%fields,
);
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};
# 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,