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

What is meant by lexical variable ? I understand all "my" variables are lexical variables. Is it a local variable ? But most of the times I see my variables declared in the beginning of the code. If its a local variable then it shd be in some subroutine. What happens if we declare variables without "my" where its declared with "my" ? I read that package variables are different from lexical variables, are there variables declared in package ? If I declare a variable and assign it some value. Now this variable happens to be in package also, will not the variable declared is program take precedence over the one in package ?
my $iwhome = TeamSite::Config::iwgethome(); my $iwmount = TeamSite::Config::iwgetmount(); ($iwcfg) = "$iwhome/bin/iwconfig"; my $dcrpath = iwpt_get_dcr_name(); $dcrpath =~ s#\.iwmnt#iwmnt#g; my $dcr_name = iwpt_get_dcr_name("basename"); my $prefix = getLOB($dcrpath); my $prefDir = uc($prefix); my $templatename = getDataType($dcrpath); my $contentname = $dcr_name; $PackageVar = 23;
If I use PackageVar in a code with value of 23, which has a vlue of 53 in package , which value will be assigned to PackageVar ?
Now when PackageVar variable is declared in package and I want the one declared in code take precedence in execution, what shall I do ? Shall I use "my" for that ?
If the variable declatred in code takes precedence over ones in package, then why do we need "my" ? Can we just not declare variables without "my" ?
Pls exlain in simple terms

Replies are listed 'Best First'.
Re: lexical variable
by moritz (Cardinal) on Oct 06, 2009 at 14:45 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.
Re: lexical variable
by almut (Canon) on Oct 06, 2009 at 15:43 UTC

    The binding of lexical (my) variables can be determined by mere lexical analysis (reading/parsing) of the program. Their binding does not depend on the runtime dynamics of the program. This is thus sometimes also called static scoping.  In contrast, there is also dynamic scoping of package variables in Perl. Both types can be employed to have "local" variables.  An example to demonstrate:

    #!/usr/bin/perl $foo = 42; sub func { print "$foo\n"; } { # new local scope my $foo = 99; # lexical variable, statically scoped func(); # -> 42 } { # new local scope local $foo = 99; # package variable, dynamically scoped func(); # -> 99 }

    The first call to func() prints 42, even though the value of $foo has been set to 99 for the local lexcial scope (the block specified by the curlies). This is because the $foo within func() is outside of that lexical scope and thus does not bind to the my $foo variable. Rather, it binds to the package variable $foo, which has been initialised to 42.

    The second call to func(), however, prints 99, because the value of the package variable $foo has been set to 99 for the local dynamic execution context of func() within the respective scope (again, the block).  In other words, the actual value that $foo within func() binds to in this case depends on the runtime dynamics of the program, which typically cannot be determined by mere lexical analysis.