in reply to Pronoun Programming

delete is a awkward choice for your example, because it is one of Perl's weird operators. Those that cannot be recreated using user code.

In most (all other?) situations, $hash{ $key } refers to a simple scalar value. Either the value of that key, or undef.

But for delete to operate, it needs to know a) the hash from which the element is to be deleted; b) the key of that element. In most languages that would necessitate passing the two identifiers (or references to them) separately: delete( \%hash, $key ); or calling a method on the hash and passing the key: %hash->delete( $key );.

Perl only gets away with the syntax it uses because of the way it's parser acts directly on the source, which allows it to see the single argument $hash{ $key } as two separate entities, not the single resultant of the combined expression.

I've often wished that Perl would allow me to take a reference to a hash element. The concept doesn't make much sense in Perl as implemented, but it would allow such constructs as: $$_ == 0 and delete $$_ for \$hash{ $key };

And exists $$_ and $$_ = 'some value' for \$hash{ $key };, and a bunch of other situations where one finds oneself having to repeat the construct.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Pronoun Programming
by plobsing (Friar) on Feb 05, 2008 at 06:02 UTC
    You could of course tie your hash to return values that allow for something similar.

    You would have to choose something other than delete $val to keep it simple, but $val->delete is almost as good. Unfortunately, this falls down on assignment. I can't think of any good way to do it ($val->set($newval) is probably as good as it gets).

    Also, you'd have to do some nasty business about garbage collection. You wouldn't want to keep an entire hash around just because you still have one element of the hash.

      I think a hash element reference would have to be a pointer to an HE struct. That contains pointers to both the key and it's value. The thing that is missing is some way to get back to the hash it is a part of, as they form a single linked list going in the wrong direction.

      There are several ways I would like to extend Perl's hashes, had I the skills and patience. This is one. Another would be to add timestamps to keys, that gets set when keys are created. That would allow for time ordered hashes which could be useful. And the same field could be used to order keys according to any arbitrary sorting order, which would be more space and time efficient than current tied hash+array hybrid solutions.

      But, as almost every attempt I've made at messing with Perl's internals has resulted in random, inexplicable crashes which my attempts to resolve have fallen on deaf ears and stoney ground, it's not something I am likely to get around to any time soon.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I tried implementing this out of curiousity. HE* DO have a pointer back to the original hash, but there's no guaranteeing that the hash is there any more.

        Thats why I tried using weakened references in stead. I would have used set/get magic if I was more comfortable with them, I never got those to work out for me before. Here's the begginings of the module (untested):