Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Two meanings of undef

by davido (Cardinal)
on Aug 16, 2020 at 03:04 UTC ( [id://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.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-18 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found