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


In reply to Re: Predeclaration by broquaint
in thread Predeclaration by hakkr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.