Question:

How can you define “my” variables scope in Perl and how it is different from “local” variable scope?

Answer:

$test = 2.3456;
 
{
 
my $test = 3;
 
print "In block, $test = $test ";
 
print "In block, $:: test = $:: test ";
 
}
 
print "Outside the block, $test = $test ";
 
print "Outside the block, $:: test = $::test ";

Output:

In block, $test = 3

In block, $::test = 2.3456

Outside the block, $test = 2.3456

Outside the block, $::test = 2.3456

The scope of “my” variable visibility is in the block only but if we declare one variable local then we can access that from the outside of the block also. ‘my’ creates a new variable, ‘local’ temporarily amends the value of a variable.


Keywords:

© 2017 QuizBucket.org