'our' does _not_ declare a lexical scoped variable!
What 'our' does is to say that the variables _name_ shall
be lexical scoped. so: use strict;
{
our $y = 17;
print "inner: $y\n";
}
print "outer: $y\n";
says that you are allowed to use the name '$y' in the block.
Its name is lexical scoped. It does enter the name $y into the stash.
Now, as you have discovered, without strict you may access
$y's value. So, how to do it with strict on ?
use strict;
{
our $y = 17;
print "inner: $y\n";
}
print "outer: $main::y\n";
strict don't let you use globals (variables that live in the
symbol table) unless:
- being told that it's actually ok (using 'our' or 'use vars')
- you fully qualify its name, as in $main::foo
So naturally, without strict you may access variables without fully
quallifying them as in your last example.
Autark.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|