[EMAIL PROTECTED] wrote:
I see. Thanks Shawn. Since we are at it, would you mind explaining a little bit
about the significance of "our" keyword. I have never understood it properly.
Most books that I referred to say that it's a "lexically-scoped global
variable". What does that mean? I understand global variables to be the ones
which are accessible to all perl program units (modules, packages or scripts).
But then, what is so lexically-scoped about it?
"our" is a declarative statement. It works like this:
use strict;
package Foo;
$bar = "Hello";
Running "perl -c" on above gives the following error:
Global symbol "$bar" requires explicit package name
This error is only raised if "use strict" is in effect (which it always
should be!) $bar is a global variable in the package Foo, so it's "fully
qualified" name is $Foo::bar, so let's fix the program:
use strict;
package Foo;
$Foo::bar = "Hello";
Ok, now this compiles without error.
Now add this:
use strict;
package Foo;
$Foo::bar = "Hello";
our $bar;
print $bar; ==> prints "Hello"
All the "our" statement does is to tell Perl that "from this point
forward, I want to be able to refer to the global variable $Foo::bar by
the short name $bar." That's it. The declaration is lexically-scoped,
which means you can refer to $Foo::bar as simply $bar at any point
between the "our" statment and the end of the innermost enclosing block,
file, or eval.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>