Broad Network


Static Attributes and Methods in Perl OOP

Perl Object Oriented Programming – Part 9

Perl Course

Foreword: In this part of the series, I talk about static attributes and static methods in Perl.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 9 of my series, Perl Object Oriented Programming. In this part of the series, I talk about static attributes and static methods in Perl. You should have read the previous parts of the series before reaching here; this is a continuation.

Static Attribute
A static attribute is an attribute whose value is the same for the class (package) and all objects instantiated from the class. A change in the value of the attribute in the class is seen in all the objects. Also a change in the value of the attribute in an object, is seen in all the other objects and the class. Remember, in Perl, a class is a package.

A static attribute can be accessed from the class without instantiation of the class.

Coding a Static Attribute
To code a static attribute, use the our modifier for the variable, in the class. Then in the constructor method, assign a reference of the variable, to an attribute in the blessed hash. The following code illustrates this:

use strict;

    {package Cla;

        our $staticProp = 8;

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

    my $obj = Cla->new();

The static variable is $staticProp. In the blessed hash, the attribute that holds the reference of the static variable is, staticPropRef. staticPropRef is also the static attribute. Both $staticProp and staticPropRef will always refer to the same value (8 in this case). Note that the our operator instead of the my operator has been used in the class description.

Accessing Static Attribute
To access the static attribute from the class, outside the class description use the following syntax:

    $ClassName::staticVariableName

To access the static attribute from the instantiated object, use the following syntax:

    ${$objectName->{'staticPropRefName'}}

In the following code, there are two instantiated objects. The static attribute value is displayed from the class and from the objects. The value is then changed from the class and displayed from the class and objects. And then the value is changed from one of the objects and displayed from the class and objects again. Read and try the code:

use strict;

    {package Cla;

        our $staticProp = 8;

        sub new
            {
                bless {staticPropRef => \$staticProp};
            }
     }

    print $Cla::staticProp, "\n";
    my $obj1 = Cla->new();
    my $obj2 = Cla->new();
    print ${$obj1->{'staticPropRef'}}, "\n";
    print ${$obj2->{'staticPropRef'}}, "\n\n";

    $Cla::staticProp = 7;
    print $Cla::staticProp, "\n";
    print ${$obj1->{'staticPropRef'}}, "\n";
    print ${$obj2->{'staticPropRef'}}, "\n\n";

    ${$obj1->{'staticPropRef'}} = 6;
    print $Cla::staticProp, "\n";
    print ${$obj1->{'staticPropRef'}}, "\n";
    print ${$obj2->{'staticPropRef'}}, "\n";

The output is:

8
8
8

7
7
7

6
6
6

Again, a static attribute is an attribute whose value is the same for the class and all objects instantiated from the class. A change in the value of the attribute from the class is seen in all the objects. Also, a change in the value of the attribute in an object, is seen in all the other objects and the class.

Static Method
A static method is a method that can be called, without instantiation of the class. You define the static method in the same way that you define non-static methods. A method defined in the normal way is a static method. You can use the double colon operator, :: or the arrow operator, -> to call the method, with the class name. If the method takes arguments, then if you use the double colon operator, $_[0] of @_ becomes the first argument; if you use the arrow operator, $_[0] of @_ becomes the name of the class. The other argument numbers follow accordingly. Read and try the following script that shows the two cases:

use strict;

    {package Machine;

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

        sub subtract0
                {
                    my $difference = $_[0] - $_[1];
                    return $difference;
                }

        sub subtract1
                {
                    my $difference = $_[1] - $_[2];
                    return $difference;
                }
    }

    my $var0 = Machine::subtract0(20,8);
    print $var0, "\n";
    my $var1 = Machine->subtract1(20,8);
    print $var1, "\n";

The output is:

12
12

To call a static method, you begin by typing the name of the class, then the double colon operator or arrow operator, then the function name and then parentheses with possible arguments.

A static method in Perl, is the same method as that of the instantiated object. The word, “static” applies to the way the method is used; that is, using without instantiation.

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