Broad Network


Copy Constructor in Perl OOP

Perl Object Oriented Programming Part 10

Perl Course

Foreword: In this part of the series, I explain how to define a copy constructor and how to copy an object in Perl OOP.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 10 of my series, Perl Object Oriented Programming. In this part of the series, I explain how to define a copy constructor and how to copy an object in Perl OOP. You should have read the previous parts of the series before reaching here; this is a continuation. A copy constructor, is a method that copies one instantiated object into another object.

How it Works
Assume that you have a class (package) from which several objects have been instantiated. The instantiated objects are blessed hashes; they all share the same methods. So, to copy an object, create an empty hash, and then copy the content of the blessed hash of the class, to the empty hash. Then bless the hash having copied content, in the same class. The two blessed hashes will share the same methods of the class.

Illustration
In the following code, the class has the normal constructor method plus the copy method. The copy method is called, copy():

use strict;

    {package Cla;

        sub new
            {
                bless {}, $_[0];
            }

        sub copy
            {
                my $copyHash = {};
                my $ref = $_[0];
                %{$copyHash} = %{$ref};  #hash copying
                bless $copyHash, "Cla";
            }
     }

    my $obj = Cla->new();
    $obj->{'num1'} = 2;
    $obj->{'num2'} = 3;

    my $copyObj = $obj->copy();

    print $copyObj->{'num1'}, "\n";
    print $copyObj->{'num2'}, "\n";

The first statement in the copy() constructor (method), creates the hash that will receive the copy. The next statement assigns the reference of the original object to the variable, $ref. The statement after copies the content of the object hash to the new hash. It is the references of both hashes that have been used in the copy statement. The last statement in the copy constructor, blesses the new hash having the copied content, into the class object. Lower in the code, the copy method of the original object is used to return the copied object. Try the above code if you have not already done so.

The following is an illustration, where the reference of a static attribute is also copied:

use strict;

    {package Cla;

        our $staticProp = 8;

        sub new
            {
                bless {staticPropRef => \$staticProp}, $_[0];
            }

        sub copy
            {
                my $copyHash = {};
                my $ref = $_[0];
                %{$copyHash} = %{$ref};
                bless $copyHash, "Cla";
            }
     }

    my $obj = Cla->new();
    $obj->{'num1'} = 2;

    my $copyObj = $obj->copy();

    print ${$copyObj->{'staticPropRef'}}, "\n";
    print $copyObj->{'num1'}, "\n";

Note: the copied object also has the copy() constructor as well as the new() constructor, since both the original hash and the copied hash share the same class (methods). In the following code, the copy constructor of the copied object is used to copy yet another object:

use strict;

    {package Cla;

        sub new
            {
                bless {};
            }

        sub copy
            {
                my $copyHash = {};
                my $ref = $_[0];
                %{$copyHash} = %{$ref};
                bless $copyHash;
            }
     }

    my $obj = Cla->new();
    $obj->{'num1'} = 2;
    $obj->{'num2'} = 3;

    my $copy1Obj = $obj->copy();

    my $copy2Obj = $copy1Obj->copy();

    print $copy2Obj->{'num1'}, "\n";
    print $copy2Obj->{'num2'}, "\n";

That is it for this part of the series. We stop here continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message