Date: Sat, 16 Sep 2000 14:30:39 -0700 From: Doug Barton <DougB@gorean.org> To: Dag-Erling Smorgrav <des@ofug.org> Cc: Mark Ovens <marko@FreeBSD.ORG>, chat@FreeBSD.ORG Subject: Re: perl(1) variable declarations Message-ID: <39C3E67F.D31B0C53@gorean.org> References: <20000915130220.C257@parish> <xzpya0so4c6.fsf@flood.ping.uio.no>
next in thread | previous in thread | raw e-mail | index | archive | help
Dag-Erling Smorgrav wrote: > > Mark Ovens <marko@FreeBSD.ORG> writes: > > What is the difference (if any) between using ``use vars'' and ``my'' > > to declare global variables? Reading perlmodlib(1) and the book > > Learning Perl they appear interchangeable, but are they? > > Variables declared with 'use vars' have global scope. They are global within that package. If you are writing just one script and don't have any 'use' or 'require' statements for other libraries or modules, than "global" is accurate since you only really have one package. However when you start including modules (or writing your own modules) package context becomes very important. > Variables > declared with my (which is actually a function) have local scope. A better way to describe it is that variables declared with my only exist within the innermost set of curly braces where they are declared. The 'main' package (your script) has an implicit set of curly braces around it. > ISTR something about top-level 'my' variables causing leaks or similar > problems in mod_perl scripts, but I don't remember the details. I haven't heard that, but I could see how it could happen. Better (well, cleaner) to declare all your global variables with 'use vars' anyway. And of course, be sure to use -w on your shebang line AND 'use strict;'. That'll cure whatever ails ya. :) HTH, Doug -- "The dead cannot be seduced." - Kai, "Lexx" Do YOU Yahoo!? To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?39C3E67F.D31B0C53>