LordWeber has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: Problems with scoping
by broquaint (Abbot) on May 06, 2003 at 16:19 UTC
    that I declare @files outside the print in the first case and inside it in the second case.
    This is because lexical variables are introduced per statement, not per expression (if that makes sense). So perl doesn't know about the lexical variable @files in the die call as the statement hasn't finished, and therefore hasn't been introduced into the surrounding lexical scope.
    HTH

    _________
    broquaint

Re: Problems with scoping
by Ovid (Cardinal) on May 06, 2003 at 16:20 UTC

    It's the reference to @files in the die statement that's causing the problem. When you declare a variable with my, our, or local, the declaration doesn't take effect until the next line. That's why you can do things like this:

    sub foo { local $_ = $_; # protect $_ and still use it ... }

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: Problems with scoping
by artist (Parson) on May 06, 2003 at 16:19 UTC
    Credit goes to the 'die' statement and you have declared $files[0] there. Use of strict prompt the compiler to check the variable declaration before it is used. Replace $files[0] by any other variable and you will get the similar error.

    artist

Re: Problems with scoping
by tall_man (Parson) on May 06, 2003 at 16:37 UTC
    I did a "perl -MO=Terse" on the sample code, and it seems that the @files is in scope only within the print statement part. The explanation may be the "if it looks like a subroutine call then it is one" rule. To the parser it looks like the @files is only declared in the print parameter list.