#!/usr/bin/perl

my $x = "global"; #declare and initialize a variable in current scope
    # In this case, this scope is known as "file level" 
    # since it is in the main file and no enclosing blocks
print "from the main area: $x\n";  #as expected will display the 
    # currently visible value of $x, being 'global'

{ #create a new block
    print "in the first block: $x\n";  #$x is as it was, 'global'
    my $x="first"; #now declare at the "block level" a variable with
        # the same name, and assign it
    print "after declaration in the first block: $x\n";  #the new $x
        # will be "seen" and "hides" the file level one so 
        # 'first' will be printed

    { #create another block, nested in the first one, this creates a
        # new block level scope.  That is a new lexical scope
        print "in the second block: $x\n"; #since we have not 
            # yet made a new $x, the previous one is used. Perl
            # "looked up".
            # This is sort of like the "enclosing" part of
            # Python's scoping.
        my $x = "second"; #now declare and assign a new block/lexical
            # level $x which will once again "hide" the 
            # previous one
        print "after declaration in the second block:$x\n";
            # 'second' will be printed 
        }

    print "after second block is closed: $x\n"; #now, we have exited
        # the inner block and so its $x has gone because the 
        # scope is closed and we will print 'first' again, 
        # since the hide of that one is gone
}
print "after first block is closed: $x\n"; #now even the first block is
    # closed and its declaration gone, and we are back to 'global'
