This is part 15 of my series, Basics of ActivePerl. In this part of the series, we look at what is called variable scope. 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. We have seen all of these constructs.
For the rest of this tutorial, we look at the if, for and sub constructs and how variable scope is applied to them. The rules outline 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.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The if Construct and Variable Scope
Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
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 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.
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;
print "Content-Type: text/html\n\n";
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;
print "Content-Type: text/html\n\n";
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 sub block cannot be seen outside the sub 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 is seen inside the for-parentheses and inside the for-block.
Conclusion
Blocks exist with different constructs. A variable declared outside blocks can be seen inside blocks. 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
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Dining in at Tender Bob's Steak House at the Block SM North EDSAOur experience on dining in at Tender Bob's Steak House one afternoon with my family. We tried out their branch at SM North Edsa which is called "The Block".
- "The Block" in Baltimore and Other Towns that Have a Similar ArrangementBaltimore's "block" is famous for being known as a place to go and see female entertainers take their clothes off. There are a large number of clubs, all in this central area. This makes it convenient for their custom...
- ActivePerl Basic SyntaxIn this part of the series, I give you the basic syntax of ActivePerl.
- ActivePerl HashesIn this part of the series I explain the meaning of Hashes in ActivePerl.
- ActivePerl Comparison and Arithmetic OperatorsIn this part of the series, we talk about some common ActivePerl operators.
- PHP Variable Scope Basics
- ActivePerl Subroutines
- False Arrest, Extortion, Ex Post Facto, Entrapment, Expediency and Police and Pros...
- PHP Function Basics
- Some JavaScript Tips
- Regular Expressions in Perl for the Novice
- Basics of ActivePerl Variables



