#/usr/bin/perl
use Devel::Peek;
use feature 'say';
my %assign = (
foo => 1234,
bar => 5678,
);
my %undef = (
foo => 1234,
bar => 5678,
);
%assign = ();
undef %undef;
say "%assign (",\%assign,") after assignment:";
Dump(%assign);
say "%undef (",\%undef,") after undef:";
Dump(%undef);
__END__
%assign (HASH(0x195f890)) after assignment:
SV = PVHV(0x193fe60) at 0x195f890
REFCNT = 1
FLAGS = (PADMY,SHAREKEYS)
ARRAY = 0x19a1d70
KEYS = 0
FILL = 0
MAX = 7
%undef (HASH(0x195f860)) after undef:
SV = PVHV(0x193fe80) at 0x195f860
REFCNT = 1
FLAGS = (PADMY,SHAREKEYS)
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
The difference lies in the ARRAY element of the above output. By assignment of an empty list the container of the key/value pairs is preserved, whereas undef erases it and sets it to a NULL pointer.
It is obviously cheaper to just wipe the container (and its content) instead of iterating through it and re-arranging its internal structure. - edit - see below, what dave_the_m writes.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
|