in reply to Understanding difference between my and local variables.

One point-of-confusion that might help to be cleared up is ... just how Perl uses the concept of namespaces.   The package statement introduces a new namespace, but a single file can have as many package statements within it as you wish.   (It may not be a nifty practice, but it can be done.)   There is a default namespace that is assumed to exist if no other namespace has been put into effect.

Therefore, everything that you declare is always associated with some namespace.   There is no “global universe outside all the spheres,” at least, not as far as I know.   (But I am a simple monk, not inducted into the Deeper Mysteries.)

Replies are listed 'Best First'.
Re^2: Understanding difference between my and local variables.
by manishrathi (Beadle) on Oct 15, 2010 at 19:53 UTC
    $a = 10; my $b = 20; # Can this be accessed inside the subroutine ? my ($c) = 30; # what is the meaning of bracket around variable name ? { # ... my ($p, @q, %r); local ($d, @e, %f); # (1) $b = 40; # does this change value of my $b ? # ... sub foo { # ... } # ... foo(17); # (2) bar($p,29); # (3) lbar($d, 31); # ... } # (4) # ... sub bar { my $d = 60; $b = 50; # ... }
    Can we access the variable "b" declared as my variable at the beginning of the code, inside the subroutine ? Or is it out of scope ? We can not access my $d, declared inside sub bar(). Likewise, can a my $b variable, which is declared outside the bar() and at the beginning of the code, be accessed inside sub bar() ?

    Thanks