#!/usr/bin/perl -w

use strict;


main();


sub main {

    my $foo = "bar";

    print "\nWe can see: \'$foo\' here\n";

    if (2+2 == 4) {
	
	print "\t And we can see \'$foo\' here!\n";
	
	if (1 == 1) {

	    print "\t\tAnd here: \'$foo\'\n";
	
	    #this variable is onle accessible from this scope down
	    my $apple = "orange";
	    
	    print "\t\tWe can see \'$apple\' here....\n";
	    
	    if (3 == 3) {
		print "\t\t\tAnd \'$apple\' is accessible here...\n";
	    }
	}
    }

    #uncomment the following line, and notice that an error is thrown.
    #this error means that the variable name $apple is not accessible in 
    #this scope
#    print "But we can't see $apple here! we've moved above it's scope!\n";


    ###Since $apple is not accessible in this scope, we can redefine it.
    my $apple = "grapes";
    print "\nAnd the variable has been initialized above the scope of the earlier init and now holds: \'$apple\'\n\n";

    
    #uncomment this after working through this block
    ####  _look_at_more_scope_issues();
}


sub _look_at_more_scope_issues {

    #notice that the variables initialized in the main subroutine
    #are not available... this is because the subroutines are conceptually
    #in parallel scopes.  Subroutines acn only access global variables, special
    #variables, or passed in variables!
    ##uncomment the following and you get a compilation error
#  print "We can't see this variable from main: $apple\n";



    #the last scope issue we'll discuss is overloading.

    #I can define a variable
    my $zip_code = "11211";

    print "My zipcode is: $zip_code\n";

    if (1 == 1) {
	#we can reinitialize $zip_code with a new value.  This new value will
	#be accessible here and to any scope below it... Above this scope, the
	#old value is still assigned......

	#EXCEPTION.  try removing the 'my' from the line below and re-running
	###if we don't re-initialize the variable here with my, then we are 
	#using the variable defined in the scope above.  If we change it, the
	#change is to the variable at the original scope...
	my $zip_code = "02143";
	print "\t down a scope level my zip code used to be $zip_code\n";
    }

    
    print "But in this scope, my zip code is still $zip_code\n\n";


}

