When I refer to a 'loop' here, I am actually generalizing to a 'block'.

As far as I know, no reason, unless you want to ensure they be available outside of the loops. Sometimes, if I'm lazy, I will declare a variable outside of said loop just to be sure that it is available someplace else. More often than not, I will assign the value of the variable inside the loop to one that exists outside.

If using a variable whose scope is only for a particular loop, I use my to ensure that it doesn't exist once the loop is done and to make sure that it doesn't conflict with any variables I might have of the same name. -- Like I said - I'm Lazy. --

As far as performance, if the loop iterates many times (I'll let you determine the number here) it would probably be better to declare the variable outside the loop. This way, you won't have to worry about the space being allocated and deallocated each time the loop iterates. This would be a case where by saving the overhead, we can improve performance.

Remember: These are only my opinions and someone may prove me wrong.

Mick

In reply to RE: Performance question by mrmick
in thread Performance question by Anonymous Monk

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.