Broad Network


Private Variable and Recursive Function in Perl

Perl Function Explained – Part 4

Perl Course

Foreword: In this part of the series, I talk about private variables and recursive function in Perl.

By: Chrysanthus Date Published: 8 Sep 2015

Introduction

This is part 4 of my series, Perl Function Explained. In this part of the series, I talk about private variables and recursive function in Perl. You should have read the previous parts of the series before coming here, as this is a continuation.

The my and our Variable Modifiers
So far in the volume, we have been declaring and defining variables as in the following examples:

    my $var;
    my $var = 5;

There is another variable modifier called, our. You can use it to do similar things as follows:

    our $var;
    our $var = 5;

The difference comes in privacy. If you modify a variable in a function block with the our operator, that variable can be seen outside the function block (and even in another block). If you modify a variable in a function block with the my operator, that variable cannot be seen outside the function block (or in another block).

To access a variable from outside its block, you can use the following syntax:

    $main::variable

The following code shows how an our variable inside a function block (func1) has been accessed from inside another block:

use strict;

    sub func1
        {
            our $va = 6;
            func2();
        }


    sub func2
        {
            print $main::va;
        }

    func1();

The output is 6. In the code, func1() calls func2().

Private Variable
The my variable cannot be accessed from outside its block (or from another block). The following code prints nothing:

use strict;

    sub func1
        {
            my $va = 6;
            func2();
        }


    sub func2
        {
            print $main::va;
        }

    func1();

So, if you want to make a variable private, modify it with the my operator in a block.

Note: this rule of privacy applies to arrays and hashes as well. The difference between the our and my operator will become clearer when you will learn the Perl Package.

Recursive Function
A recursive function is a function that calls itself. It calls itself repeatedly until a condition is satisfied and then it stops. If the condition is not satisfied, it may call itself forever. The following code prints the word, “seen” 5 times.

use strict;

    my $counter = 0;

    sub func
        {
            our $var = "seen ";
            print $var;
            $counter+=1;
            func() if $counter < 5;
        }

    func();

The function is called once at the end of the code. The first line in this function block defines a variable with the value, “seen” using the our modifier. The second line prints the variable. The third line increments the counter. The fourth line calls the function again as long as the value of the counter is less than 5. “if $counter < 5” is the condition for the recursion to end. Note that the function can have arguments.

When dealing with recursive function, the variables should be the our variables. In this way you end up with one copy (instance) of a variable. If you use my, you will end up with multiple copies (instances) of the same variable; that is, one copy per function pass. If the passes are many, the program may crash because memory gets full. So, the following code works, but it is not recommended.

use strict;

    my $counter = 0;

    sub func
        {
            my $var = "seen ";
            print $var;
            $counter+=1;
            func() if $counter < 5;
        }

    func();

To proof this, try the following code, which prints the reference of the my variable:

use strict;

    my $counter = 0;

    sub func
        {
            my $var = "seen ";
            print $var, \$var;
            $counter+=1;
            func() if $counter < 5;
        }

    func();

I tried the code and I had the following:

seen SCALAR(0xe2fb2c)seen SCALAR(0x3f7e14)seen SCALAR(0xe28284)seen SCALAR(0xe283a4)seen SCALAR(0xe3018c)

A reference is like the address of the variable. Note that the references are different, confirming that each function pass has its own copy of the variable, ending up with multiple copies of the variable (value).

Now try the following code, which has the same purpose as the above:

use strict;

    my $counter = 0;

    sub func
        {
            our $var = "seen ";
            print $var, \$var;
            $counter+=1;
            func() if $counter < 5;
        }

    func();

I tried the code and I had:

seen SCALAR(0xe2fc7c)seen SCALAR(0xe2fc7c)seen SCALAR(0xe2fc7c)seen SCALAR(0xe2fc7c)seen SCALAR(0xe2fc7c)

Note that the reference of the variable for each pass is the same, confirming that memory is not wasted.

Conclusion
If a variable is declared with the modifier, our, in a block, that variable can be seen outside the block. If a variable is declared with the modifier, my, in a block, that variable cannot be seen outside the block. In the case of recursion, if the variable is declared with our, only one copy of the variable will exist for each function pass; if the variable is declared with my, multiple copies of the variable will exist, which is not really good.

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