variable declaration, was RE: moving to the next line

From: Chris Devers (cdevers_at_pobox.com)
Date: 12/17/04


Date: Fri, 17 Dec 2004 09:17:05 -0500 (EST)
To: "Charles K. Clarkson" <cclarkson@htcomp.net>

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.

Out of curiosity, why this rule?

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.

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?

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

Thanks :-)

-- 
Chris Devers


Relevant Pages

  • Re: variable declaration, was RE: moving to the next line
    ... Declare them as you go. ... > the Perl I've written. ... In some other languages, like C, C++ and Java you must specify ...
    (perl.beginners)
  • Re: Recurse over a hash without knowing any elements (except top level)
    ... > I have a hash that is several levels deep. ... You forgot to declare $depth. ... "use strict" would have told you about ... By choosing not to "use strict" you instruct Perl to assume any ...
    (comp.lang.perl)
  • Re: help: having trouble evaluating expressions with EVAL
    ... picking up a good perl book at this point or finding a good tutorial ... That's the whole point of using strict. ... Declare all your variables, using 'my', and surround all your strings ... all it does is forbid you from using global variables ...
    (comp.lang.perl.misc)
  • Re: variable declaration, was RE: moving to the next line
    ... It works in other languages, ... Declare them as you go. ... > it's something that I've generally done with the Perl I've written. ... > of defeating the point of using 'strict': ...
    (perl.beginners)
  • Re: hi Mr.John Bokma
    ... some Perl error messages. ... When you include "use strict" you are making a promise to perl, ... You are promising to declare all of your variables, ... They are called "headers" because they are supposed to come ...
    (comp.lang.perl.misc)