On Fri, 17 Dec 2004 09:17:05 -0500 (EST), Chris Devers
<[EMAIL PROTECTED]> 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.
> 
> 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.

And what programming language were you learning?  C requires
all variable declarations to come first, for the benefit of the compiler.
[This requirement may not apply to the latest C specification]

In some other languages, like C, C++ and Java you must specify
the exact type of each variable.  This information is useful to gather
in to one place, out of the way of the actual code.

In perl, you have signals ($%@&* etc) to specify the type, and my
to declare that it is a new lexical variable.  You don't have to specify
if you have a char etc.

Also in perl, each block is a new lexical scope - where all is fair to
introduce new variables.  E.g.

for my $index (0 .. $last_index) {
    my $computed = ...;

    ...
}

where $index and $computed wouldn't exist outside of the loop.  This
is easier to read than:

my $index;
my $computed;

for $index (0 .. $last_index) {
    $computed = ...;

    ...
}

Where $index and $computed live on, but may never be used again.

Generally, fewer lines of code are easier to manage.  That is until you
start writing obfuscated perl code, but you usually need to be skilled to
do that :-)  Just see my JAPH (below) or search google for others!

> Perl is an eccentric language to be sure, ...

Not as much as the programmers that use it ;-)

Jonathan Paton

-- 
#!perl
$J=' 'x25 ;for (qq< 1+10 9+14 5-10 50-9 7+13 2-18 6+13
17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
00+4 00+8 1-21 01+1 00+5 01-7 >=~/ \S\S \S\S /gx) {m/(
\d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,

-- 
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