Auto local variable : Variable Scope « Language Basics « Perl






Auto local variable

    

#!/usr/bin/perl
use warnings;
use strict;

my $var = 42;
my $last;
print "Before: $var \n";
foreach $var (1..5) {
    print "Inside: $var \n"; # print "Inside: 1", "Inside: 2" ...
    $last = $var;
}
print "After: $var \n"; # prints '42'
print "Last: $last \n";

   
    
    
    
  








Related examples in the same category

1.Local element
2.Local variables.
3.Forward Reference
4.Uses the same name inside and outside a foreach statement.
5.Creates a global variable $myvar and prints out the global instance of the variable and the package-specific variable:
6.Scope of Variables: Variables used in subroutines are global by default