in reply to Predeclaration

Does pre declaring variables before you use them have any memory/performance benefit for the interpreter by providing type information.
Essentially no. There are several reasons for this one of which is that perl isn't a strongly typed language so the compiler isn't designed to take advantage of 'type hints'. In fact it is every so slightly quicker to *not* predeclare your variables in terms of execution speed as it increases the amount of operations to be performed
use Benchmark qw(cmpthese); ## take the results with a grain of salt, mmmkay? cmpthese(-15, { predeclare => sub { my $x = {}; $x->{foo} = "bar" x 20 }, nodeclare => sub { my $x; $x->{foo} = "bar" x 20 } }); __output__ Benchmark: running nodeclare, predeclare, each for at least 15 CPU sec +onds... nodeclare: 24 wallclock secs (15.33 usr + 0.00 sys = 15.33 CPU) @ 11 +0465.75/s (n=1693440) predeclare: 18 wallclock secs (15.58 usr + 0.02 sys = 15.60 CPU) @ 89 +620.83/s (n=1398085) Rate predeclare nodeclare predeclare 89621/s -- -19% nodeclare 110466/s 23% --
So as you can see the sub *without* the anonymous hash assignment was ever so slightly faster, but I'd say the difference is fairly negligible.
I know in other strongly typed languages you should always declare the type of your variable but does it matter in perl or am I doing it by prepending @ or % or $.
There are only really two types in perl and they are scalars and lists and everything else from there on out is an abstraction. For more information on strongly-typed languages and perl see. Ovid's Griping about Typing, and for info about data types in perl see the perldata manpage.
Also is putting a 'my' call inside a loop to clear the variable advisable?
I think you may have a misunderstanding of what function my() performs. Declaring a variable with my() will create a new variable in the current lexical scope, that's all. So you're not 'clearing' the variable you're just creating a new variable in the current lexical scope e.g
my $foo = "a string"; print "\$foo is $foo\n"; ## this $foo only lasts to the end of the for loop scope for my $foo (1..3) { print "\$foo is $foo\n"; } print "\$foo is $foo\n"; __output__ $foo is a string $foo is 1 $foo is 2 $foo is 3 $foo is a string

HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Predeclaration
by hakkr (Chaplain) on Jul 16, 2002 at 11:27 UTC
    Thanks broquaint guess I'll stop doing it then. My question now is why does anyone bother to predeclare if it has no effect or an adverse effect.

    I suppose it must just be for improved readability and to help the stongly typed converts

      People generally predeclare with my to assist with error-checking. If you add the pragma use strict; to the top of your program, all variables have to be declared with my or our before they're used. This helps eliminate errors due to variable name typos.

      For example:

      use strict; my $typo; $tuypo = "Here's some data: $data"; print $typo;
      will produce an error.