Broad Network


Perl Package

Perl Packages Part 1

Perl Course

Foreword: In this part of the series, I talk about the Perl Package and the effects of the my and our modifiers to variables in the package.

By: Chrysanthus Date Published: 15 Sep 2015

Introduction

This is part 1 of my series, Perl Packages. In this part of the series, I talk about the Perl Package and the effects of the my and our modifiers to variables in the package. A package is a set of one or more of the following variables: scalar, array, hash and subroutine. These variables should work together. They are in expressions (statements). With the exception of subroutine (non-reference function), each of these variables can be preceded by my or our.

Packages in Perl can be classified in two ways, which I call: the Block Type Package and the Module Type Package.

Pre-Knowledge
This series is part of a Perl course. At the bottom of this page you will see the different series you should have read before coming here.

The Block Type Package
A block is a set of statements enclosed in curly brackets.

Package Example
The following program has a package with two variables and two functions, which are expected to work together. Note that the our modifier has been used for the scalar variables. The package is in a block.

use strict;

    {package Pack;

        our $scal1 = 1;
        our $scal2 = 2;

        sub funct1
            {
                print "I am function $scal1.", "\n";
            }
        sub funct2
            {
                print "I am function $scal2.", "\n";
            }
    }

    print $Pack::scal1, "\n";
    print $Pack::scal2, "\n";

    Pack::funct1();
    Pack::funct2();

Try the code, if you have not already done so. The output is:

1
2
I am function 1.
I am function 2.

The package begins with { and ends with }. Just after the opening { you have:

    package Pack;

This indicates that what you have in the block is a set of related variables, in a package, called, Pack. This line ends with a semicolon. There are two scalar variables in the package and two functions. The first function prints a sentence in which it expands (is replaced by value) the first scalar variable. The second function prints a sentence in which it expands the second scalar variable.

Note: it is the subroutine name that is considered as the variable, not the subroutine (function) body.

Outside the package, to access a scalar variable, you use the syntax:

    $PackageName::scalarName

Note the use of the double colon operator, and where $ has been placed (just in front of the package name and not in font of the scalar name).

Outside, to access an array, you use the syntax:

    @PackageName::arrayName

Outside, to access an array element, use the syntax:

    $PackageName::arrayName [index];

Note the preceding $.

Outside, to access a hash, use the syntax:

    %PackageName::hashName

Outside, to access a hash value, use the syntax:

    $PackageName::hashName{'key'}

Note the preceding $.

Outside the package, to call a function, use the syntax,

    PackageName::functionName(args)

Note: when calling a function, you can type -> instead of the double colon, :: , as in

    PackageName->functionName(args)

Effect of the my Modifier
The above example uses the our modifier on variables. With that, the variables could be seen (accessed) outside the package. If the variable modifier is my, then the variable will not be seen outside the package. Try the following code and note that the values of the variables are not printed (but the functions are called):

use strict;

    {package Pack;

        my $scal1 = 1;
        my $scal2 = 2;

        sub funct1
            {
                print "I am function $scal1.", "\n";
            }
        sub funct2
            {
                print "I am function $scal2.", "\n";
            }
    }

    print $Pack::scal1, "\n";
    print $Pack::scal2, "\n";

    Pack::funct1();
    Pack::funct2();

Whether the variables are preceded by my or our, functions are always called outside the package.

The my modifier makes a variable (be it scalar or array or hash) local to the block (package) and the variable cannot be accessed from outside the block. However, functions can be accessed, since they are not preceded by my or our. The my modifier makes the variable private to the block.

Special Variables
Special variables such as $? and $! are in a package called, main. You use them anywhere in the program without any prefix. You do not see the main package but you just use its variables.

Module Type Package
In this section, I talk about the kind of package which spans the line of declaration to the end of the file without the block; I also say the effects of the my and our modifiers on the variables in the package. A package is a set of one or more of the following variables: scalar, array, hash and subroutine. These variables should work together. They are in expressions (statements). With the exception of subroutine (function), each of these variables can be preceded with my or our.

Package Example
Try the following code:

use strict;

package Pack;

    our $scal1 = 1;
    our $scal2 = 2;

    sub funct1
        {
            print "I am function $scal1.", "\n";
        }
    sub funct2
        {
            print "I am function $scal2.", "\n";
        }

    print $Pack::scal1, "\n";
    print $Pack::scal2, "\n";

    Pack::funct1();
    Pack::funct2();

The output is:

1
2
I am function 1.
I am function 2.

The code is the same as the first code in the previous part of the series, but without the package block (curly brackets). The output is the same. However, here, everything beginning from the package declaration line to the end of the file is in the package.

The package declaration line is,

    package Pack;

same as in the block type package, ending with a semicolon (but there is no block here). What determines the start of the package is the declaration line, and not { for the block type package. What ends this package, is the end of the file. In this case, the preceding package names to access the variables and for the function calls, are like redundant. The following accesses and calls will yield the same result:

    print $scal1, "\n";
    print $scal2, "\n";

    funct1();
    funct2();

Effect of the my Modifier
The above example uses the our modifier on variables. With that, the variables could be seen (accessed) outside the package. If the variable modifier is my, then the variable will not be seen outside the package. Now, the above package ends at the end of the file, so it is not possible to test the effect of the my modifier (operator). The Perl manual actually indicates that the module type package ends at the end of the file or at the end of the eval block  Try the following code which indicates how the eval block looks like:

use strict;

    eval
        {
            package Pack;

            my $scal1 = 1;
            my $scal2 = 2;

            sub funct1
                {
                    print "I am function $scal1.", "\n";
                }
            sub funct2
                {
                    print "I am function $scal2.", "\n";
                }
        }

    &Pack::funct1();
    &Pack::funct2();

The output is:

I am function 1.
I am function 2.

I have not been able to access the my variables. Since the module type package is in the eval block, the function calls have been preceded by &.

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

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message