Broad Network


Basics of Perl Variable Scope

Perl Basics – Part 15

Perl Course

Foreword: In this part of the series, I talk about variable scope in Perl.

By: Chrysanthus Date Published: 29 Mar 2015

Introduction

This is part 15 of my series, Perl Basics. In this part of the series, I talk about variable scope in Perl. 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, for, foreach, and sub. You should have seen all of these constructs. You should have read the previous parts of the series before reaching here, as this is a continuation.

For the rest of this tutorial, I discuss the if, for and sub 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. As you try the code samples in this tutorial, you may receive error messages; do not worry about the error messages for now.

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 for 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";

for (my $i=0; $i< 3; ++$i)
    {
        print $hisVar;
        my $herVar = "her scalar";
    }

#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 inside the parentheses of the for-construct be seen outside the for 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 parentheses can be seen outside the for-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 parentheses of a for-construct is not seen outside the for-construct, but it can be seen inside the for-parentheses and inside the for-block.

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

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. Remember, all the principles outlined in this tutorial series work with traditional Perl.

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

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message