Constants and Accessors in Perl OOP
Perl Object Oriented Programming – Part 8
Perl Course
Foreword: In this part of the series, I explain how to code constants and I also explain the recommended way to access attributes.
By: Chrysanthus Date Published: 26 Sep 2015
Introduction
Constant in a Class
To have a constant in a class, use the constant function. For a public attribute, just use a public constant function. For a private attribute, use a code reference constant function. See illustrations below.
Accessors
You have the get() accessor for a class. It is similar to the protected getParent() method for a subclass; however, instead of restricting access to the subclass, the access is restricted to the parent class.
You have the set() accessor for a class. This too is similar to the protected getParent() method for a subclass; however, instead of restricting access to the subclass, the access is restricted to the parent class. See illustrations below.
Code illustrations
The following code illustrates how to code and access a public constant, pi and a public attribute:
use strict;
{package TheCla;
sub pi () {3.14159};
sub get
{
return undef unless (ref($_[0]) eq "TheCla");
return $_[0]->{$_[1]};
}
sub new
{
bless {word1 =>"pen", word2=>"book"}, $_[0];
}
}
my $obj = TheCla->new();
print $obj->pi, "\n";
print $obj->get("word1");
The output is:
3.14159
pen
To obtain the value of the constant, you just call the constant method in the normal way. To obtain the value of a public attribute, you should code and use the get() method. The get() method restricts access only to its class’ object (not to the child object). Note the first statement in the get() method definition, that has, TheCla, which is the name of its class. The return statement of the get() method definition, uses the object reference to access the blessed hash. So, you cannot use the class name with this method to access the blessed hash.
The following code illustrates how to code and access a private constant, pi and a private attribute:
use strict;
{package TheCla;
my %ha = (wordA => "pencil", wordB => "ruler");
$ha{pi} = sub () {3.14159};
sub pi () {3.14159};
sub get
{
return undef unless (ref($_[0]) eq "TheCla");
return $ha{$_[1]};
}
sub new
{
bless {word1 =>"pen", word2=>"book"}, $_[0];
}
}
my $obj = TheCla->new();
print &{$obj->get("pi")}, "\n";
print $obj->get("wordA");
The output is:
3.14159
pencil
The get function here restricts access only to the class object. So, the class name cannot be used to access the attributes. This particular get method can be used to access the code reference of any private method, but remember, that the code reference has to be dereferenced as with the private constant here.
The following code illustrates how to access a private and a public attribute:
use strict;
{package TheCla;
my %ha = (wordA => "pencil", wordB => "ruler");
sub pi () {3.14159};
sub get
{
return undef unless (ref($_[0]) eq "TheCla");
if (exists $ha{$_[1]})
{
return $ha{$_[1]};
}
elsif (exists $_[0]->{$_[1]})
{
return $_[0]->{$_[1]};
}
}
sub new
{
bless {word1 =>"pen", word2=>"book"}, $_[0];
}
}
my $obj = TheCla->new();
print $obj->get("wordA"), "\n";
print $obj->get("word1");
The output is:
pencil
pen
The following code sets (changes) the value of a private variable and the value of a public variable:
use strict;
{package TheCla;
my %ha = (wordA => "pencil", wordB => "ruler");
sub pi () {3.14159};
sub set
{
return undef unless (ref($_[0]) eq "TheCla");
if (exists $_[3] eq "private")
{
$ha{$_[1]} = $_[2];
return $ha{$_[1]};
}
else
{
$_[0]->{$_[1]} = $_[2];
return $_[0]->{$_[1]};
}
}
sub new
{
bless {word1 =>"pen", word2=>"book"}, $_[0];
}
}
my $obj = TheCla->new();
my $priv = $obj->set("wordA", "eraser", "private");
my $pub = $obj->set("word1", "ink");
print $priv, "\n", $pub;
The output is:
eraser
ink
Note that the get() and set() methods of the class are public methods. However, they restrict access to the attributes only to objects of the class (not of a different class).
Typical get() Method
A typical get method for private and public attributes should be as follows:
sub get
{
return undef unless (ref($_[0]) eq "TheCla");
if (exists $ha{$_[1]})
{
return $ha{$_[1]};
}
elsif (exists $_[0]->{$_[1]})
{
return $_[0]->{$_[1]};
}
}
Typical set() Method
A typical set method for private and public attributes should be as follows:
sub set
{
return undef unless (ref($_[0]) eq "TheCla");
if (exists $_[3] eq "private")
{
$ha{$_[1]} = $_[2];
return $ha{$_[1]};
}
else
{
$_[0]->{$_[1]} = $_[2];
return $_[0]->{$_[1]};
}
}
The reason why, access has been restricted to the object of the class and not the class (name), is because in many situations, it is the attributes of the object that are accessed and not the variables of the package. When you want to access the variables of the package (class description) use the static approach – see later.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Perl BasicsPerl 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