They declare a name.

Yes true (the term is usally identifier I beleive). And this is the source of confusion in my opinion. In perl we mean declaration to mean what it means in english. Unfortunately unless you are lucky enough to have learned programming with Perl in the first place this contradicts the meaning implied in most other languages in that a declaration both defines a name and creates a thing. Since this interpretation is pretty well uniform outside of perl our use of it in its "classical" sense is a problem for newbies. The ones that come from other languages get confused and think that our creates something (it doesn't necessarily), and the ones that learn Perl in the first place and then move on are in for a shock (although probably much less of one than the other way around.)

This is further reenforced on those occasions when my doesn't cause a new piece of memory to be allocate....In both these cases, my isn't creating anything. It is declaring a name that is used to refer to a pre-existing piece of storage for the duration of a level of scope.

I think that if you rethink this you will realize that the situation is far from this simple. First off the two examples are very different. The first involves creating a copy of every key, and then aliasing the variable name $key to each one in turn. Space is allocated for $key, but as the name is aliased that space is unused until some later assignment. But it is certainly allocated.

The while example is probably closer to what you meant, but even there you have a problem. Neither you, nor perl, can determine if the my variable can be optimized to only use one memory slot until after the entire scope is complete. If at any point an aliase or reference to the variable is made the optimizer has to leave the behaviour of my alone, otherwise it can safely optimise it to reuse a single slot. But the point here is that it is the optimizer that is responsible for my not creating a new var, and not the intrinsic behaviour of my.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: Re: On Declaration by demerphq
in thread On Declaration by demerphq

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.