Broad Network


Public Attributes and Methods in Perl OOP

Perl Object Oriented Programming – Part 4

Perl Course

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

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 4 of my series, Perl Object Oriented Programming. In this part of the series, I talk about public 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 seen outside its package, then that member is a public member of the class. If a member cannot be seen (accessed) outside its package (class), then that member is a private member. You should have read the previous parts of the series before coming here, as this is a continuation.

Class Example
Read and try the following code:

use strict;

    {package TheCla;

        sub fn1
            {
                print "first function", "\n";
            }
        sub fn2
            {
                print "second function", "\n";
            }

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

    my $obj = TheCla->new();

Nothing should have been printed (if you tried the code). The question is, “Can the attributes and methods be accessed outside this package using the class name or object reference?” I will present the answer in two phases: using the class name and using the object reference.

Before we continue, know that the attributes and methods of all the classes (and corresponding objects) defined in this series so far, are public members. I spend the rest of this tutorial justifying that.

If a member cannot be accessed outside its package then it is a private member.

“Outside the Class” here, means in the main program outside the package, and in a subclass (inherited class).

Access with Class Name
We shall consider the main program and a subclass (derived class).

In the above code, the new() method has already been accessed successfully in the main program with TheCla->new(). This expression uses the class name, TheCla and returns an object reference held by $obj (you can use some other name for $obj).

Add the following code to the bottom of the above code (try the resulting code), and note that the method, fn1() has been accessed within the subclass, which is outside the Thecla package, using the class name:

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

        sub childFn
            {
                TheCla->fn1();
            }
    }

    Child->childFn();

The method, fn2() can similarly be accessed.

These modifications show that the functions (methods) defined in the normal way using the name of the subroutine, are public.

Now, the way the above code has been written is such that you cannot use the class name to access an attribute. If the hash in the constructor had been assigned to an our variable, then it would have been possible to access the object and its attributes using the class name; however I will not discuss that.

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 when a class is coded in the normal way, its attributes and methods are public.

Replace the added code above with the following and try the resulting code. You will notice that the attributes and methods of the class have been accessed using the object reference, in the main program, outside the class.

    print $obj->{word1}, "\n";
    $obj->fn1();

The attribute, word2 and the method, fn2() can similarly be accessed.

Replace the added code above with the following and try the resulting code. You will notice that the attributes and methods of the parent class (object) have been accessed, as well as inherited, using the subclass object reference in the inherited object, outside the parent class.

    {package Child;

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

    }

    my $childObj = Child->new();

    print $childObj->{word2}, "\n";
    $childObj->fn2();

The attribute, word1 and the function, fn1 can similarly be accessed.

The attributes accessed with the subclass (object), are copies of a parent object.

Before we leave this part of the series, know that a method defined in the normal way in a class, is defined using the function name. A method defined with code reference in a class is not defined in the normal way, and so has to be called differently – see later. An attribute instantiated with an anonymous hash, is a public attribute. A method defined in the normal way is a public method.

Let us stop here for this part of the series 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