my $a = 1;
my $contrived = sub {
my $a = 2; # new lexical variable $a
$a = 3; # if the my above wasn't there, outer $a would be 3
...
}
say $a; # 1
Explicit declaration of the scope of a variable at initialisation is awesome. 'global' and 'nonlocal' feel weird/less consistent/like a workaround. People are used to creating variables 'on the fly' in Python / CoffeeScript - both break (for a certain value of 'break') certain assumptions in certain circumstances.I've never heard anyone ever complain about perl's lexical 'my'.. It even catches - with a warning - multiple my declarations that will clobber each other. You don't see that in other languages that often.