Broad Network


Connecting Perl Packages

Perl Packages – Part 2

Perl Course

Foreword: In this part of the series, I explain how to connect packages with the @ISA array and the parent pragma; I also talk about the symbolic connection with the double colon.

By: Chrysanthus Date Published: 15 Sep 2015

Introduction

This is part 2 of my series, Perl Packages. In this part of the series, I explain how to connect packages with the @ISA array and the parent pragma; I also talk about the symbolic connection with the double colon. You should have read the previous part of the series before coming here, as this is a continuation.

The @ISA Array
Any package contains the @ISA array. Read and try the following code with two packages:

use strict;

    {package PackA;

        our $scalA = 'A';


        sub functA
            {
                print "I am function A", "\n";
            }
    }

    {package PackB;

        @PackB::ISA = ("PackA");

        our $scalB = 'B';


        sub functB
            {
                print "I am function B.", "\n";
            }
    }

    PackB->functA();

At the end of this code, PackB has been used to call a function in PackA and the output is,

    I am function A.

The function call is,

        PackB->functA();

and not  PackB::functA(). When two packages have been connected, in order to use the child package to call a function in the parent package, you use -> and not :: . To use a package to call a function in the same package, you use :: or -> .

Each package has its own @ISA array. If you want to use PackB to call a function in PackA, then you have to go into PackB description (not PackA) and add the "PackA" string in the list of values of PackB’s @ISA array, as follows:

        @PackB::ISA = ("PackA");

Under this condition, you cannot use PackB to access a variable in PackA; you can only use PackB to call a function in PackA, and with ->.

You can have more than one parent package in the @ISA statement as in,

        @PackB::ISA = ("PackA", "PackZ");

You separate the package strings with commas.

That parent Pragma
The @ISA array is used when the packages are in the same file. If the parent packages are in separate files, but in the same directory as your main program, then you have to use the parent pragma instead, at the position of the @ISA statement. The syntax for a single parent is:

    use parent "parentName";

So, if PackA above was in a different file, in the working directory, then the following program is equivalent to the above:

use strict;

    {package PackB;

        use parent "PackA";

        our $scalB = 'B';


        sub functB
            {
                print "I am function B.", "\n";
            }
    }

    PackB->functA;

The parent pragma actually loads the file package into memory and sets the @ISA array (without you knowing). If there are more than one parent package in different files, then use the pragma as follows:

       use parent "PackA", "PackZ";

You separate the package strings with commas in the pragma.

Symbolic Use of the Double Colon
The double colon is, :: . In the first code above, PackB became the sub-package of PackA, as a result of the @ISA array in PackB. In your code, you can represent the access of a variable in PackB as follows:

    $PackA::PackB::scalB;

This kind of expression is ignored by Perl. PackA and PackB will always remain independent packages. This kind of expression does not operate and does not lead to any access. It is a symbolic expression, which can be typed in code (program).

That is it for this part of the series.

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

Comments

Become the Writer's Fan
Send the Writer a Message