in reply to Re^3: Scope, package, and 'my' variables
in thread Scope, package, and 'my' variables

In my original question, yes, that makes sense. But in my clarifying question, in which there are braces around the code in package bbb, why doesn't that set up an independent version of $var which then should get picked up by do_b and thus print out "second time for var"?

Thanks

package bbb; { use strict; use warnings 'all'; my $var = 'second time for var'; sub do_b { print "In 'bbb' var is set to '$var'\n"; } }

Replies are listed 'Best First'.
Re^5: Scope, package, and 'my' variables
by chromatic (Archbishop) on Jan 05, 2005 at 04:52 UTC

    It does declare a new lexical $var over which do_b closes. However, you call the subroutine before the assignment occurs.

Re^5: Scope, package, and 'my' variables
by Errto (Vicar) on Jan 05, 2005 at 04:57 UTC
    The issue is that the assignment of $var within the block under bbb never happens before you call do_b. Don't think of a line like my $x = somevalue; as a declaration of a variable to be initialized before the program begins. Think of it as a statement that runs when the program gets to it.