You shouldn't be using my on variables that another
piece of code somewhere else ever might need to access.
my is strictly for variables that will never need to
be accessed from anywhere else -- loop counters and
internal stuff that other code shouldn't mess with.
You've probably read or heard somewhere from someone
an oversimplistic comment to the effect that you
should declare all your variables with my, but if so
that is fundamentally wrongheaded. You should
declare variables with my when you want them
to be accessible exclusively from the current code
block (i.e., lexically scoped). (Or when you want
to create a closure, but closures are beyond the
scope of this discussion.) It especially makes
no sense to declare all your varibles with my after a
package declaration, because the package declaration
already accomplishes the thing that is usually the
major goal of lexical scoping (i.e., to keep other
code from _accidentally_ tromping your variables
or having theirs tromped by your code). There are
certainly
cases where _certain_ variables should be scoped
with my inside a package, or even most of them,
but if you're doing it to
every single varibale without thinking, don't.
The Coping with Scoping article that chmrr suggested
is (mostly) excellent, and you should read
it if there's any part of what I said above that you
don't fully understand. It neglects to explain why
you would _want_ other code to be able to expressly
alter your variables, but from your post it appears
that you already understand that part. It also does
not explain dynamic scope 100% correctly (and in fact
is dead wrong on at least one point), but you don't
need to understand dynamic scope in order to correctly
use lexical and package scope. You _do_ need to
understand the differences between package scope
and lexical scope, and it explains that very well,
so I won't repeat the explanation here.
Go read it.
{my$c;$ x=sub{++$c}}map{$ \.=$_->()}map{my$a=$_->[1];
sub{$a++ }}sort{_($a->[0 ])<=>_( $b->[0])}map{my@x=(&
$x( ),$ _) ;\ @x} split //, "rPcr t lhuJnhea eretk.as
o";print;sub _{ord(shift)*($=-++$^H)%(42-ord("\r"))};