Sorry, I sent the last reply before I had finished it.

On Fri, Dec 17, 2004 at 09:17:05AM -0500, Chris Devers wrote:

> On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
> 
> >     Don't declare all your variables at the beginning
> > of the script. It works in other languages, but not
> > in perl. Declare them as you go.

I suppose that depends on your definition of "works".

> Out of curiosity, why this rule? 

Locality of reference.  If accesses of nearby memory are close in time
we have locality of reference.  We can help to achieve this by ensuring
that use of variables are localised to as small a section of code as
possible.  This has the added and arguably more important advantage of
making the program easier to understand and maintain because it might
only be necessary to understand small sections of it at a time.

> When taking programming classes in college, it was drummed into us that 
> having a data dictionary at the top of a scope was a good habit, and 
> it's something that I've generally done with the Perl I've written. 

Curious.  In some languages this is necessary but I think most people
see it as a deficiency in the language rather than the enforcement of
good style.

> Several people on this list have discouraged the habit. How come?
> 
> I can see the logic in discouraging global variables, but predeclaring 
> variables at the top of a scope -- the beginning of a subroutines, and a 
> small handful in the main block -- still seems acceptable to me. Indeed, 
> waiting to declare until the variable is used is, to my thinking, kind 
> of defeating the point of using 'strict': if the declarations are 
> scattered all over the place, why bother being strict? 

strict vars is designed to stop you accidently misspelling a variable
name.  You are right in a way in that if all your uses of a variable are
close together there is less chance that you will misspell one of them.
but using strict vars will still catch you if you do.

> Perl is an eccentric language to be sure, but why over this? Anyone care 
> to explain? 

I don't know that this is specific to Perl.  C++, for example, is
probably best written in this way too.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to