From owner-freebsd-chat Sat Sep 16 14:30:59 2000 Delivered-To: freebsd-chat@freebsd.org Received: from dt051n37.san.rr.com (dt051n37.san.rr.com [204.210.32.55]) by hub.freebsd.org (Postfix) with ESMTP id B865237B423; Sat, 16 Sep 2000 14:30:56 -0700 (PDT) Received: from gorean.org (doug@master [10.0.0.2]) by dt051n37.san.rr.com (8.9.3/8.9.3) with ESMTP id OAA49475; Sat, 16 Sep 2000 14:30:40 -0700 (PDT) (envelope-from DougB@gorean.org) Message-ID: <39C3E67F.D31B0C53@gorean.org> Date: Sat, 16 Sep 2000 14:30:39 -0700 From: Doug Barton Organization: Triborough Bridge & Tunnel Authority X-Mailer: Mozilla 4.75 [en] (X11; U; FreeBSD 5.0-CURRENT-091 i386) X-Accept-Language: en MIME-Version: 1.0 To: Dag-Erling Smorgrav Cc: Mark Ovens , chat@FreeBSD.ORG Subject: Re: perl(1) variable declarations References: <20000915130220.C257@parish> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org Dag-Erling Smorgrav wrote: > > Mark Ovens 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