in reply to Scoping question

in an expression such as

my $item = $item;
The first $item on the line is the one you're declaring lexical while the second $item is whatever $item is available in the enclosing scope. Your lexical $item isn't available until after that statement has completely executed.

Perl 6 will change this behavior and make variables available as soon as possible. In order to get the old behavior you'll need to do something like:

my $item = $OUTER::item;

Replies are listed 'Best First'.
Re^2: Scoping question
by pg (Canon) on Nov 03, 2004 at 21:00 UTC

    As a matter of fact, this even does not work:

    use strict; use warnings; my $item = $item;

    You will get:

    Global symbol "$item" requires explicit package name at a.pl line 4. Execution of a.pl aborted due to compilation errors.

    Update:

    see bpphillips's reply to my post, duff could actually meant what dpphillips detailed below.

      I think your interpretation of duff's example is different than intended. Consider:
      use strict; use warnings; my $item = "a"; if(1){ my $item = $item; }
      This compiles fine with no errors...

        Note that you don't need to have if (1). A block is fine all by itself, and does enforce a new scope. A bare block is a loop construct though while an if block isn't, which is important to remember when you use e.g. last.

        ihb

        See perltoc if you don't know which perldoc to read!
        Read argumentation in its context!