in reply to lexical variable

With "lexical" (in the context of programming) people usually mean "determined while reading the program", in contrast to "determined while running the program".

In this context it means that the variable is valid up to the end of the scope it is declared it. If you declare it at the top of the file, it's known in all of the file. If you declare it within a function or a block, it's only known in that function/block and in inner blocks of that block.

Package variables, in contrast, can also be accessed from other files.

It makes sense to declare all of your variables and use strict;. That way Perl will find typos in variables for you that otherwise take hours to find.

Update: we've had quite a few discussions and tutorials about scoping already, maybe you find some of them enlightening:

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: lexical variable
by manishrathi (Beadle) on Oct 06, 2009 at 15:50 UTC
    1) If I declare a variable $first in a FirstFile.pl at the top without "my" , does it become package variable ? What package does it belong to ? How can it be accessed in SecondFile.pl ?
    2) If a variable is declared as "$first" and another variable is declared as "my $second" in FirstFile.pl at the beginning of the program, can "my $second" variable be used in subroutines of FirstFile.pl ? I guess "$first" can be used in subroutines of the FirstFile.pl
    3) $first is declared as package variable. But what package does it belong to ? How this variable be accessed in SecondFile.pl ? I guess "my $second" can not be used in SecondFile.pl

      See also the replies to main package

      And did you actually read the nodes I linked to, and the documentation? It explains quite a bit.

      Perl 6 - links to (nearly) everything that is Perl 6.