develooper Front page | perl.beginners | Postings from March 2002

Re: Global Variables: 'my' vs 'use vars'

Thread Previous
From:
Chas Owens
Date:
March 28, 2002 13:55
Subject:
Re: Global Variables: 'my' vs 'use vars'
Message ID:
1017352321.2090.67.camel@tert.icallinc.com
On Thu, 2002-03-28 at 14:55, eric-perl@pretorious.net wrote:
> Hello, All:
> 
> I've never been very good at scoping so it it's no surprise that this 
> confuses me:
> 
>   When declaring variables at the beginning of a script, what is the 
>   difference between 'my' and 'use vars'?
> 
> -- 
> Eric P.
> Los Gatos, CA

The big difference is that 'use vars;' doesn't produce warnings when
used with Getopt::Std <grin>.  I think it boils down to the fact that
use vars is compiler directive (pragma) that prevents specific warnings
from being issued and my is a function that declares a local variable. 
Variables get declared when they are used by default in Perl (unlike
some languages like C).  This can be a good thing if you only need 10
lines of code and don't want to waste time typing; however, it is a very
bad thing in larger programs (ie you meant to type @commands but typed
@command instead and it simple gets created).  This is why they added
warnings to tell you about one use variables and various other
warnings.  But that was not enough; some programmers still had tons of
errors, so they added 'use strict;' which forces you to do some things
that just make sense for large projects.

Also note that use vars has been superseded by the our command in later
versions of perl (5.6 and up). 

<snip href="perldoc vars">
NOTE: The functionality provided by this pragma has been
superseded by "our" declarations, available in Perl v5.6.0
or later.  See the our entry in the perlfunc manpage.
</snip>



-- 
Today is Boomtime the 14th day of Discord in the YOLD 3168
This statement is false.

Missile Address: 33:48:3.521N  84:23:34.786W


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About