Broad Network


Private Attributes and Methods in Perl OOP

Perl Object Oriented Programming – Part 5

Perl Course

Foreword: In this part of the series, I talk about private visibility of attributes and methods for a Perl class and corresponding object.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 5 of my series, Perl Object Oriented Programming. In this part of the series, I talk about private visibility of attributes and methods for a Perl class and corresponding object. An attribute or method can be called a member of the class and corresponding object. If an attribute or method can be accessed only inside its class and object, but not outside the class and object, then that member is a private member of the class. You should have read the previous parts of the series before reaching here, as this is a continuation.

Code Example
Read and try the following code:

use strict;

    {package TheCla;

        my %ha = (wordA => "pencil", wordB => "ruler");
        $ha{coderef} = sub
                    {
                        print "I have a $ha{wordA} and a $ha{wordB}", "\\n";
                    };

        sub meth
            {
                &{$ha{coderef}}();
            }

        sub new
            {
                bless {word1 =>"pen", word2=>"book"}, $_[0];
            }
    }

    my $obj = TheCla->new();

    $obj->meth();

A private member of a class is a member that cannot be seen (accessed) outside its class. It can be accessed only by members of the class. A simple way to have private attributes in a class is to place the attributes in a separate hash in the class; then precede the hash with the my modifier. In that way, the hash and its elements cannot be seen outside the package. If you want private methods, then place the methods inside the hash using code reference (and not subroutine names). Such private methods should not be placed within the parentheses of the hash; they should be placed outside the parentheses. In that way they can use the members of the separate (private) hash.

The method, meth() in the class above is a public method, but it uses the private method, coderef. This is an example of a method within the class using another method still within the class. Outside the class, after instantiation, this public method has been called (accessed). The pubic method prints,

    I have a pencil and a ruler

It is the public method that has called the private method. The private method has not been called (accessed) from outside the class, and cannot be accessed as an individual method from outside the class. Note that the private method has been able to access the private attributes from within the class.

As I said in the previous part of the series, the normal blessed hash and the normal named functions are the public hash and public methods. A hash preceded by my with attributes and code reference methods produce private members that can be seen by both the private and public members of the same class (but not outside the class).

I demonstrate the meaning of private members below, by trying to access them outside the class in the main program and in another package (inherited class). I approach this in two phases: accessing with the class name and accessing with the object reference. The object variable holds a reference to the object instantiated from the class.

Access with Class Name
Add the following code to the bottom of the above program and run the resulting complete program:

    print $TheCla::ha{wordA};

The private variable, wordA has not been accessed (printed) using the class name in the main program. However, if the private hash had been preceded by the modifier, our, making it public, it would have been accessed.

Replace the added code with the following and run the complete program again and note that you receive an error message:

    &{$TheCla::ha{coderef}}();

The private method has not been accessed (called) on its own from outside the class, using the class name. The private method is a method in the private hash. If the hash had been preceded by our, making it public, then the method would have been called (accessed).

Replace the added code above with the following and notice that the private attribute, wordB has not been accessed, using the parent class name in a derived class:

    {package Child;
        
        @Child::ISA = ("TheCla");

        sub childMeth
            {
                print $TheCla::ha{wordB};
            }

    }

    Child::childMeth();

The attribute has not been accessed because it belongs to a hash that is preceded by my. If the hash had been preceded by our, the attribute would have been accessed.

Replace the added code above with the following and notice that you receive an error message.

    {package Child;
        
        @Child::ISA = ("TheCla");

        sub childMeth
            {
                &{$TheCla::ha{coderef}}();
            }

    }

    Child::childMeth();

It has not been possible to access the parent private method in the subclass (inherited class) using the class name of the parent class because the private method is in a hash preceded by my. If the hash was preceded by our, then the method would have been accessed.

Access with Object Reference
What the constructor returns, is actually the reference of the object and not the object (hash) itself. Again I will use the main program and the inherited class, to prove that an element coded in a class hash preceded by my, is private and cannot be accessed outside its package.

Replace the added code above with the following and note that the private attribute, wordA has not been accessed outside the class, using the object reference:

    print $obj->{wordA};

Replace the added code above with the following and notice that the private attribute, wordB has not been accessed, using a parent object reference in the subclass:

    {package Child;
        
        @Child::ISA = ("TheCla");

        sub childMeth
            {
                print $obj->{wordB};
            }

    }

    Child::childMeth();

Replace the added code above with the following and notice that you receive an error message.

    {package Child;
        
        @Child::ISA = ("TheCla");

        sub childMeth
            {
                &{$obj->ha{coderef}}();
            }

    }

    Child::childMeth();

It has not been possible to access the parent private method in the subclass (inherited class) using the object reference of the parent class.

Initialization in Parent Class
With the Perl OOP scheme I am presenting to you, a private attribute cannot be initialized in the constructor, otherwise it will be seen outside its class.

That is it for this part of the series. We stop here and 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