Broad Network


Block and Perl Scope

Perl Scoping – Part 1

Perl Course

Foreword: In this part of the series, I talk about the effect of the block in Perl scoping.

By: Chrysanthus Date Published: 8 Nov 2015

Introduction

This is part 1 of my series, Perl Scoping. In this part of the series, I talk about the effect of the block in Perl scoping. A scope is where a variable can be accessed. A block is a set of statements enclosed in curly braces, which are { and }. The question here is: if a variable is declared outside a block will it be seen in the block? On the other hand, if it is declared inside the block, will it be seen outside the block? Blocks do not occur arbitrarily in code. There are certain constructs that have blocks. The following constructs have blocks: if, foreach, while-loop, sub, package and eval. You should have seen some of these constructs.

For the rest of this tutorial, I discuss the if, sub, foreach, anonymous-block and package constructs and how variable scope is applied to them. The rules outlined in this tutorial are applicable when the statement, “use strict;” is used at the top of the code. They are also applied to any other block not mentioned here. As you try the code samples, you may receive error messages; do not worry about the error messages for now.

Pre-Knowledge
This series is part of the volume, Perl Course. At the bottom of this page, you will find links to the different series, you should have read before coming here.

The if Construct and Variable Scope
Read and try the following code:

use strict;

my $hisVar = "his scalar";

if (25 == 25)
    {
        print $hisVar;
        my $herVar = "her scalar";
    }

#print $herVar;

The if condition is if 25 is equal to 25. Now this condition will always return true, and so the if block will always be executed. Outside the if-block, the variable, $hisVar is declared and assigned the value, "his scalar". Inside the if-block there is a statement to print $hisVar. This variable was declared outside the block; if it is seen inside the block, it will be printed. If you tried the code you would have noticed that the value of $hisVar was printed.

Now, inside the block, a new variable, $herVar was declared and had a value assigned to it. Outside the block, there is a comment. This comment is actually a print statement preceded by the comment denotation, #. Because of this preceding sign, the statement is not executed. If you remove the comment and re-try the code, the following explanation will follow:

The $herVar variable is declared inside the block. Now, if it is seen outside the block, then the last statement (without the comment denotation) would print its value. Remove the # symbol and try the code and note that the last print statement would not work, and you would probable receive an error message.

Also note: a variable declared outside the block, can be seen in the condition (parentheses) of the if-construct.

The sub Construct and Variable Scope
The following code has been written similar to the above; the variables and test (print) statements have been written in a similar way.

Read and try the following code:

use strict;

my $hisVar = "his scalar";

sub myFn
    {
        print $hisVar;
        my $herVar = "her scalar";
    }

myFn;

#print $herVar;

You should have tried the code. Note that variable declared outside the sub-block is seen inside the sub-block.

Now remove the comment denotation in the last line and try the code again; you will probably receive an error message, because a variable declared inside the sub block cannot be seen outside the sub block.

The foreach Construct and Variable Scope
The following code has been written similar to the above; the variables and test (print) statements have been written in a similar way.

Read and try the following code:

use strict;

my $hisVar = "his scalar";

foreach my $i (0..2)
    {
        print "$i $hisVar\n";
        my $herVar = "her scalar\n";
    }

#print $herVar;
#print $i;

You should have tried the code. Note that the variable declared outside the for-block is seen inside the for-block. In this case the value of the variable is printed 3 times.

Now remove the comment denotation in the last-but-one line and try the code again; you will probably receive an error message, because a variable declared inside the for-block cannot be seen outside the for-block.

Put back the comment symbol, you have just removed. There is a new question: Can a variable declared in the expression of the foreach modifier (i.e. on the right of the foreach word) be seen outside the foreach construct (block)? To verify this, remove the comment symbol in the very last line of the above code. If the variable, $i which has been declared in the expression can be seen outside the foreach-construct, then the last statement (line) will display it. Try the code and note that the variable is not seen outside the for-construct; you would probably receive an error message.

A variable, declared inside the expression of a foreach modifier is not seen outside the for-construct, but it can be seen inside the expression and inside the for-block. In Perl foreach and for are synonymous.

Also note: a variable declared outside the for-construct can be seen in the expression of the for modifier.

Anonymous Block
Anonymous means, no name. Yes, in Perl, a block can be anonymous. Read and try the following code:

use strict;

    my $hisVar = "his scalar";

        {
            print "$hisVar";
            my $herVar = "her scalar";
        }

    #print $herVar;

The block has no name. Note that a variable declared outside the anonymous block is seen inside the blockblock.

Now remove the comment denotation in the last line and try the code again; you will probably receive an error message, because a variable declared inside an anonymous block cannot be seen outside the block.

Package
There are two kinds of package definition (description). You have the package with a block and the package without a block. Here, I talk about the package with a block. Try the following package code:

use strict;

    my $hisVar = "his scalar";

        {package Pack;

            print "$hisVar\n";
            our $herVar = "her scalar";
        }

    print $Pack::herVar;

A variable declare outside the package (block) can be seen inside the package.

A variable declared inside a package can be seen outside the package, under two conditions: 1. The variable has to be declared with the our variable modifier. 2. Outside the package, the variable has to be accessed by inserting PackageName:: between $ and the text name of the variable.

Conclusion
Blocks exist with different constructs. A variable declared outside a block can be seen inside the block. A variable declared inside a block cannot be seen outside the block, everything being equal.

We continue in the next 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

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message