[EMAIL PROTECTED] wrote:

> Does the use strict; impact the "my $x;" declaration any differently?

no. 'my $x;' creates a lexical variable $x which is local to the block $x is 
declared no matter you 'use strict;' or not use strict.

> 
> What does "our $x;" do?
> 

perldoc -f our

> in any case, usig the same variable name such as $x in the main and other
> sub would occur as a temporary place holder. However, I was curious on how
> to access the outer var in the sub whcih you desc with $main::x

this only works if $x is a global variable. global variable lives in the 
symble table of the package they declared. the package 'main' is the 
default package that any variable implicitly lives in if you haven't 
specify another package with 'package AnotherPackage'. some simple example:

#!/usr/bin/perl -w
use strict;

               our $x = 1; #-- implicitly in the main package
package Apple; our $x = 2; #-- explicitly in the Apple package
package Dog;   our $x = 3; #-- explicitly in the Dog package

print 'first print:  ', $x,        "\n";
print 'second print: ', $main::x,  "\n";
print 'third print:  ', $Apple::x, "\n";
print 'fourth print: ', $Dog::x,   "\n";

__END__

prints:

first print:  3
second print: 1
third print:  2
fourth print: 3

all these are explained in the 2 docs i mentioned from my previous article.

> 
> Ok - I'm trying to figure out what this means as declared in the main:
>> 'my $x="abc";' creates a local variable visible in the whole script.

variables declared with 'my' are local to the lexical scope they declared 
in. you can create an explicitly lexical scope by creating a block:

#--
#-- { ... a new block ... }
#--
{
        my $x = 1;

        #--
        #-- $x is said to be local to this block only
        #--
} 

#--
#-- knows nothing about $x because it's outside of the block that $x
#-- is declared in. in fact, if you have 'use strict;', Perl complains
#-- and won't even compile the following.
#--
print $x,"\n";

Perl always search for the inner most lexical block for variable resolution 
so the following always shadow the outer local $x:

my  $x = 1; #-- local variable
our $x = 2; #-- global variable

{
        my $x = 3;

        #--
        #-- no way to access the outer local $x whose value is 1
        #--

        #--
        #-- this, however, access the outer global $x whose value is 2
        #-- because 'main::' tells Perl where to start search for $x.
        #-- in this case, we are start searching for $x from the package main
        #-- which escapes the lexical block
        #--
        print $main::x,"\n";
}

a subroutine, of course, careates a lexical block as well so the concept 
applies to subroutine as well. that's why if you have 2 variables with 
exactly the same name and if the variables are both declared as local or 
global, you won't be able to access the outer most one.

>> '   $x="abc";' creates a global visible inside and outside the script.

see reasons above.

> 
> I've tested the my $x; inside the sub and it behaves as I expected. But in
> the main, I'm still not 100% sure what is the diff?

see reasons for diff above.

> 
> Does global means like static variable in C?
> 

yes and no depends how you use them. read the docs i mentioned for reasons.

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to