Wednesday, January 9, 2013
11:00 PM

Perl Script: Difference between local and my variables

'my' creates a new variable (but only within the scope it exists in), 'local' temporarily amends the value of a variable

ie, 'local' temporarily changes the value of the variable (or global variable), but only within the scope it exists in.

So with local you basically suspend the use of that global variable, and use a "local value" to work with it. So local creates a temporary scope for a temporary variable.

Below script explains the concepts of local Vs my variables



Source : cat local_my.pl
#!/usr/bin/perl

print " ----- local example ------ \n";
$a = 10;
{
  local $a = 20;
  print "Inside block \$a    : $a \n";
  print "Inside block: \$::a : $::a \n";
}

print "Outside block \$a   : $a \n";
print "Outside block: \$::a: $::a \n";
Continue Reading...

0 comments:

Post a Comment