Not 'wrong headed', just a compiler compiler optimization.  By putting
the declarations before the usage, you reduced the number of complete
passes through the source by one and made the parsing code easier.
This is an artifact from the time when Machine-Time was expensive and
Programmer-Time was (relatively) cheap.

The locality-of-reference concept ('Declare It In the Most Limited
Scope') came out of the discovery that declaring variables so that
they were known throughout the entire program lead to either
interesting bugs (when you got a stale value of your variable, for
example), or ugly naming practices (intLoopIndex, intLoopIndexOne,
intLoopIndecTwo, etc).


Old Gray Bear

On Sun, Jan 4, 2009 at 6:34 AM, Telemachus <telemac...@arpinum.org> wrote:
> On Sat Jan 03 2009 @ 11:00, John W. Krahn wrote:
>>>> David Newman wrote:
>>> I always found it "cleaner", and have heard others say it's preferable,
>>> to declare all variables at the top of the program (although admittedly
>>> I didn't do that even in this short script).
>>
>> It is always better to limit the scope of variables.  If variables need
>> to be in file scope anyway then a few lines one way or the other
>> probably won't make that much difference.
>
> So, I'm curious about this now. I've also read the advice always to declare
> all variables at the top of the program (in C books, perhaps, or shell
> script books and tutorials?). But Perl-centered advice always seems to be
> the other way around: limit the scope of variables; declare things only
> when you need them (and in the smallest scope possible). The second
> approach makes sense to me, and I use it, but I'm wondering what motivated
> the original approach. Was it simply a wrong-headed attempt to be "logical"
> - all the variables go here, then main, then the functions, etc.?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to