in reply to Re: On Declaration
in thread On Declaration

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...