<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:Courier New,courier,monaco,monospace,sans-serif;font-size:10pt"><div style="font-family: arial,helvetica,sans-serif;">Thanks for the input. I guess I took somethings for granted and didn't read up the relevant sections carefully :)<br><br>I am not nitpicking here but I would like to share a few more things which became more obvious to me - hopefully it will help others too.<br><br>New code follows<br>======================================================================<br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">$ cat a.pl </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">use strict;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
 New,courier,monaco,monospace,sans-serif;"><br>push @INC, '.';</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>our $c = 15;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>require newbie;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;">======================================================================<br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>$ cat newbie.pm </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">package newbie;</span><br style="font-family: Courier
 New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>print "first attempt: " . $c . "\n";</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">print "second attempt: " . $::c . "\n";</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>1;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;">======================================================================<br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br>$ perl a.pl </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
 New,courier,monaco,monospace,sans-serif;">first attempt: </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">second attempt: 15</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;">======================================================================<br><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">$ perl -w a.pl </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">Use of uninitialized value $newbie::c in concatenation (.) or string at newbie.pm line 3.</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">first
 attempt: </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">second attempt: 15<br></span>======================================================================<br><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">$ perl -v<br>This is perl, v5.10.0 built for x86_64-linux-thread-multi<br></span>======================================================================<br><br>I am just demonstrating that it is <span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">use warnings</span> which would help more in this scenario rather than use strict.<br><br>Something else which confused me after I started understanding our better was the following - maybe someone can point out if I have miss-understood anything.<br><br>As per perldoc for
 our<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "our" associates a simple name with a package variable in the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; current package for use within the current scope. When "use<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strict 'vars'" is in effect, "our" lets you use declared global<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; variables without qualifying them with package names, within the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lexical scope of the "our" declaration. In this way "our"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; differs from "use vars", which is package scoped.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Unlike "my", which both allocates storage for a variable
 and<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; associates a simple name with that storage for use within the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; current scope, "our" associates a simple name with a package<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; variable in the current package, for use within the current<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scope. In other words, "our" has the same scoping rules as "my",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; but does not necessarily create a variable.<br><br>The first paragraph seems to imply to me that our is for declaring a global variable as most C developers tend to think of them. If I understand it correctly our is always associated with a package - if we call our outside a defined package as is the case in my sample it goes to the package main.<br><br><span>The
 second paragraph doesnt help me understand things any better other than to say that my and our are similar in scope. I would like to quote from <a target="_blank" href="http://stackoverflow.com/questions/845060/what-is-the-difference-between-my-and-our-in-perl">http://stackoverflow.com/questions/845060/what-is-the-difference-between-my-and-our-in-perl</a> which says</span><br><br><div style="margin-left: 40px;">Available since Perl 5, my is a way to declare:<br><div style="margin-left: 40px;">&nbsp;&nbsp;&nbsp; * non-package variables, that are<br>&nbsp;&nbsp;&nbsp; * private,<br>&nbsp;&nbsp;&nbsp; * new,<br>&nbsp;&nbsp;&nbsp; * non-global variables,<br>&nbsp;&nbsp;&nbsp; * separate from any package. So that the variable cannot be accessed in the form of $package_name::variable.<br></div><br>On the other hand, our variables are:<br><div style="margin-left: 40px;">&nbsp;&nbsp;&nbsp; * package variables, and thus automatically<br>&nbsp;&nbsp;&nbsp; *
 global variables,<br>&nbsp;&nbsp;&nbsp; * definitely not private,<br>&nbsp;&nbsp;&nbsp; * nor are they necessarily new; and they<br>&nbsp;&nbsp;&nbsp; * can be accessed outside the package (or lexical scope) with the qualified namespace, as $package_name::variable.<br style="font-family: arial,helvetica,sans-serif;"></div></div><br style="font-family: arial,helvetica,sans-serif;"></div><div style="font-family: Courier New,courier,monaco,monospace,sans-serif; font-size: 10pt;"><span style="font-family: arial,helvetica,sans-serif;">Personally the second explanation is much more easier for me to comprehend than the perldoc but I suppose someone could elaborate what I might be missing out on if I follow that explanation.</span><br style="font-family: arial,helvetica,sans-serif;"><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">In any case thanks for listening to me and hopefully I am not making a
 fool of myself here with perl basics.</span><br style="font-family: arial,helvetica,sans-serif;"><br><br style="font-family: arial,helvetica,sans-serif;"><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">- Mithun</span><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><font face="Tahoma" size="2"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Alexander Danel &lt;danel@speakeasy.net&gt;<br><b><span style="font-weight: bold;">To:</span></b> Chicago.pm chatter &lt;chicago-talk@pm.org&gt;<br><b><span style="font-weight: bold;">Sent:</span></b> Wed, March 3, 2010 9:00:08 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [Chicago-talk] Global variable behavior<br></font><br>
<meta http-equiv="x-dns-prefetch-control" content="off">


 
 

<style>
<!--
 
 _filtered {font-family:Tahoma;panose-1:2 11 6 4 3 5 4 4 2 4;}
 
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:purple;text-decoration:underline;}
span.EmailStyle17
        {font-family:Arial;color:navy;}
 _filtered {margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
        {}
-->
</style>



<div class="Section1">

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Get in the habit of putting the following
at the top of every file:</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> &nbsp;</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use
strict;</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> &nbsp;</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Try it.&nbsp; It will inform you that you
are implicitly doing just what you claim you are not doing: you have declared
(or, as you put it “redeclared”) the variable.&nbsp; “Use
strict” will protest because implicit declarations are often an accident;
which is clearly appropriate for your case.</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> &nbsp;</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">By the way, I take exception to your
statement &nbsp;“. . . should make it global across all name spaces.”&nbsp;
No such concept exists in Perl.&nbsp; You declared the variable to be in the “main”
namespace; which you seemingly understood when you used “$::c”, the
naked double-colon being an alias for “main::”.&nbsp; &nbsp;The
package “main” is in force until you declare otherwise.</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> &nbsp;</span></font></p> 

<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Alexander Danel</span></font></p> 

<div>

<div class="MsoNormal" style="text-align: center;" align="center"><font face="Times New Roman" size="3"><span style="font-size: 12pt;">

<hr tabindex="-1" size="2" width="100%" align="center">

</span></font></div>

<p class="MsoNormal"><b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma; font-weight: bold;">From:</span></font></b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;"> chicago-talk-bounces+danel=speakeasy.net@pm.org
[mailto:chicago-talk-bounces+danel=speakeasy.net@pm.org] <b><span style="font-weight: bold;">On Behalf Of </span></b>Mithun Bhattacharya<br>
<b><span style="font-weight: bold;">Sent:</span></b> Wednesday, March 03, 2010
8:20 PM<br>
<b><span style="font-weight: bold;">To:</span></b> chicago-talk@pm.org<br>
<b><span style="font-weight: bold;">Subject:</span></b> [Chicago-talk] Global
variable behavior</span></font></p> 

</div>

<p class="MsoNormal"><font face="Times New Roman" size="3"><span style="font-size: 12pt;"> &nbsp;</span></font></p> 

<div>

<div>

<p class="MsoNormal"><font face="Arial" size="3"><span style="font-size: 12pt; font-family: Arial;">Hi Everyone,<br>
<br>
A piece of my code is behaving in a way I didn't expect and I was hoping
someone could enlighten me as to why..<br>
</span></font><br>
-------------------------------------<br>
<font size="2"><span style="font-size: 10pt;">$ cat <a target="_blank" href="http://a.pl">a.pl</a> <br>
push @INC, '.';<br>
<br>
our $c = 15;<br>
<br>
require newbie;<br>
</span></font>-------------------------------------<br>
<font size="2"><span style="font-size: 10pt;">$ cat <a target="_blank" href="http://newbie.pm">newbie.pm</a> <br>
package newbie;<br>
<br>
print "first attempt: " . $c . "\n";<br>
print "second attempt: " . $::c . "\n";<br>
<br>
1;<br>
</span></font>-------------------------------------<br>
<font size="2"><span style="font-size: 10pt;">$ perl a.pl <br>
first attempt: <br>
second attempt: 15</span></font><br>
-------------------------------------<br>
<font face="Arial"><span style="font-family: Arial;"><br>
The way I see it the </span></font><font face="Courier New"><span style="font-family: &quot;Courier New&quot;;">our</span></font><font face="Arial"><span style="font-family: Arial;"> in </span></font><font face="Courier New"><span style="font-family: &quot;Courier New&quot;;">a.pl</span></font><font face="Arial"><span style="font-family: Arial;"> should make it global across all namespaces. The
second attempt does seem to say that did take place. What I am not sure is why
the </span></font><font face="Courier New"><span style="font-family: &quot;Courier New&quot;;">$c</span></font><font face="Arial"><span style="font-family: Arial;"> variable has become local in scope
even though I haven't re-declared or redefined it in any way inside </span></font><font face="Courier New"><span style="font-family: &quot;Courier New&quot;;">newbie.pm</span></font><font face="Arial"><span style="font-family: Arial;">.<br>
<br>
<br>
<br>
<br>
- Mithun</span></font></p> 

</div>

</div>

<p class="MsoNormal"><font face="Times New Roman" size="3"><span style="font-size: 12pt;"> &nbsp;</span></font></p> 

</div>

<meta http-equiv="x-dns-prefetch-control" content="on"></div></div>
</div><br>

      </body></html>