in reply to On Declaration

I think that your looking at this from the wrong angle.

I would say the my, our, package and sub all declare something. They declare a name.

The fact that in some cases, that name will refer to a piece of storage that may already exist our, or a new piece of storage that will be created my, or to a potentially pre-existing, namespace in which other names can be grouped package, or a name of a piece of code, that will exist within current, potentially implicitly named, namespace sub, could be considered a side-effect of the declaration to the compiler that I wish to use a particular name at the current level of scope.

This is further reenforced on those occasions when my doesn't cause a new piece of memory to be allocate. Eg.

for my $key ( keys %hash ) {...} while( my $line = <FILE> ) {...}

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.

In all cases, the four keywords declare names that the programmer wishes to use for some entity. That sometimes that entity may be a pre-existing one and other cases it will be created is (almost) incidental.

The joker in the pack of course is local, which says I wish to use a pre-existing name to refer to a new piece of storage.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: On Declaration
by demerphq (Chancellor) on Jun 16, 2003 at 09:50 UTC

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