http://qs321.pair.com?node_id=11120801


in reply to Two meanings of undef

#!/usr/bin/env perl use strict; use warnings; print "Before assignment: \$xyz ", (exists($::{'xyz'}) ? 'exists' : 'd +oes not exist'), "\n"; eval '$main::xyz = 42;'; print "After assignment: \$xyz ", (exists($::{'xyz'}) ? 'exists' : 'do +es not exist'), "\n"; eval 'print $main::xyz, "\n";'; # Prints 42. eval 'undef $main::xyz;'; print "After undef: \$xyz ", (exists($::{'xyz'}) ? 'exists' : 'does no +t exist'), "\n";

This produces:

Before assignment: $xyz does not exist After assignment: $xyz exists 42 After undef: $xyz exists

The reason I used string eval is because if Perl catches wind of $xyz in the source code it already will populate the symbol table. By using eval I'm able to defer Perl's knowledge of it until just in time.

The point is that undef $xyz; doesn't remove the entry from the symbol table. Presumably you could do it manually: delete $::{'xyz'};, but you'll also obliterate any other entities that share the same symbol. Possible candidates include a hash, an array, a filehandle, or code, if any exist in the same namespace by the same name.


Dave