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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pronoun Programming
by plobsing (Friar) on Feb 05, 2008 at 06:02 UTC | |
by BrowserUk (Patriarch) on Feb 05, 2008 at 09:06 UTC | |
by plobsing (Friar) on Feb 05, 2008 at 14:32 UTC | |
by BrowserUk (Patriarch) on Feb 06, 2008 at 12:02 UTC | |
by plobsing (Friar) on Feb 07, 2008 at 01:56 UTC |