You should always declare variables with either our or my.
"whichever package statement is in effect".Here's how you set the package.
use strict;
use warnings;
# the default namespace is main
# so this is a variable in main.
our $movie='Chitty Chitty Bang Bang';
# set the current package
package A::B::C;
# We've changed the package - it is no longer main
# now its A::B::C
# if a variable is undefined in the current package,
# then Perl looks for a variable in main of the same name
# these two statements are the same and do not cause
# warnings
print "movie=$movie\n";
print "movie=$main::movie\n";
# but once we declare the variable in A::B::C, an
# unqualified name is presumed to be in the current
# package
# declare a package variable in the namespace A::B::C
our $movie = 'Mary Poppins';
# these print different things
print "movie=$movie\n"; # prints Marry Poppins
print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang
{
# this overrides the current package. The override
# will last until the next matching curly brace
package NotA::NotB;
# this is $movie but in the NotA::NotB namespace
# its a different variable from either $main::movie
# or $A::B::C::movie
my $movie = 'Pippi Longstocking';
print "movie=$movie\n"; #prints Pippi Longstocking
print "movie=$A::B::C::movie\n"; #prints Mary Poppins
print "movie=$main::movie\n"; #prints Chitty Chitty Bang Bang
# } matches { enclosing package statement
# so this ends the code block where NotA::NotB is the
# package.
}
# now we are back to A::B::C being the current package
print "movie=$movie\n"; # prints Marry Poppins
print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang
Hope that helps.
Best, beth |