Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Two meanings of undef

by davido (Cardinal)
on Aug 16, 2020 at 03:04 UTC ( #11120801=note: print w/replies, xml ) Need Help??

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.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120801]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2021-09-18 02:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found