On Aug 2, 2005, at 8:31 PM, Louis Pouzin wrote:
While tracking some mysteries in a script behavior I hit an
interesting feature.
"DELETE hash element" seems to create one, when it's missing. It may
be normal
and well known in the Perl milieu, but new to me.
delete $H{$ip};
# delete $H{$ip}{any}; # create $H{$ip}
It looks as if Perl processes DELETE as follows:
1- create the missing element
2- delete the terminal element, as requested
3- leave behind the higher structure
The moral: leave the dead alone, for they haunt you.
When you refer to elements that don't exist, one of two things happens,
depending on whether it's in an lvalue context or not, i.e. whether or
not you're modifying it.
my %foo;
my $x = $foo{bar}{baz}; # $x has the undefined value. %foo remains
empty.
$foo{bar}{baz} = 1; # %foo contains one key, 'bar', whose value is a
reference to a hash containing a key 'baz'
delete $foo{bar}; # Deletes the only reference to the second hash,
causing it to be destroyed. %foo is now empty.
delete $foo{bar}{baz}; # Auto-vivifies $foo{bar} as a reference to an
empty hash.
Once you delete $foo{bar}, you can't use it to refer to anything that
existed previously. If it was the only reference to the second hash,
you're done. If not, you'll need to delete the deeper element first.
Josh
--
Joshua Juran
Metamage Software Creations - Mac Software and Consulting
http://www.metamage.com/
* Creation at the highest state of the art *