5.22. Scope of variables

Variables defined within a block {} are hiding more global
variables with the same name. 
They are descrutced when leaving the block.

Example 5-15. examples/scalars/scope.pl

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

my $fname = "Foo";
my $lname = "Bar";
print "$fname\n";        # Foo
print "$lname\n";        # Bar

{
    my $email = 'foo@bar.com';
    print "$email\n";     # foo@bar.com
    print "$fname\n";     # Foo
    print "$lname\n";     # Bar

    my $lname  = "Moo";
    print "$lname\n";     # Moo
}
# $email does not exists
print "$fname\n";         # Foo
print "$lname\n";         # Bar


If you are interested in on-site trainings by the author, please contact me directly.

Online courses: