Hello all! The following questions assume that "use strict" is enabled.

Question 1:
In general, I would think it is better to do this:

my($foo); for ($i = 0; $i < $a_huge_value; $i++) { $foo = getValue(); codeThatUsesFoo($foo); }

Than this:

for ($i = 0; $i < $a_huge_value; $i++) { my $foo = getValue(); codeThatUsesFoo($foo); }

Because in the first case you're reusing the same memory space over and over, and in the second you're creating memory space in every iteration.

True, or not so much? I'm just looking for a standard practice kind of thing here.

Question 2:

Is there a way to do this:

my($key); my($value); for ($i = 0; $i < $a_whole_lot; $i++) { my($key, $value) = returnsAnArray(); }
Without creating locally-scoped copies of $key, and $value? In other words, I'm using "my" here to create the anonymous array, but I'm ending up recreating locally-scoped $key and $value at the same time, which I'd rather not do for the reasons outlined in question 1. Other than making the array named rather than anonymous, is there something I'm missing?

Apologies all around for these beginner questions.

CT

Charles Thomas
Madison, WI

In reply to Two Questions on "my" by C_T

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.