Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: What delete from symbol table really means? (Deleting typeglob of a specified package)

by LanX (Saint)
on Feb 16, 2015 at 14:25 UTC ( [id://1116871]=note: print w/replies, xml ) Need Help??


in reply to What delete from symbol table really means? (Deleting typeglob of a specified package)

short

You only deleted a stash entry pointing to the glob, not the glob which continues to exist as long as it is referenced in code.

long

A typeglob is a kind of hash with 6 fixed slots holding different data types² !

The scalar $n is the value referenced in the scalar slot of of the glob *n ¹

Think of a stash as a HoH  $pck{globname}{SCALAR} = \ 'value'

And like all Perl data *n is a C structure somewhere in memory and can be retrieved by its reference GLOBREF = \*n.

A symbol table is a hash holding names and references of such typeglobs.

Eg in your case

 %Foo:: = ( ..., "n"=> GLOBREF, ... )

The code print $n internally compiles to something like  print GLOBREF->{SCALAR} where GLOBREF was looked up in the stash at compile time and hardcoded into the OP-codes.

simplified analogy

globs and stashes are confusing, I tried to translate what is happening to HoHs.

Maybe that makes it clearer

BEGIN { # at compile time %stash =(); # 'package stash' $stash{n}{SCALAR} =undef; # $n first use ( i.e. our-declaration ) $n_glob = $stash{n}; # exists only hardcoded } # at runtime: execute compiled op-codes $n_glob->{SCALAR}=123; delete $stash{n}; print $n_glob->{SCALAR}; # > 123

So deleting the Stash entry didn't delete the underlying glob (it's still referenced)

Update: You only sabotaged introspection at run time or further compilations with eval('print $n') .

Cheers Rolf

PS: Je suis Charlie!

update

¹)

DB<104> $n=123 => 123 DB<105> *n{SCALAR} => \123

²) i.e. refs to SCALAR, ARRAY, HASH, CODE, IO, GLOB, FORMAT see perlref

Replies are listed 'Best First'.
Re^2: What delete from symbol table really means? (Deleting typeglob of a specified package)
by LanX (Saint) on Feb 16, 2015 at 18:06 UTC
    Though I have to admit that it's a bit confusing that a typeglob keeps track of it's original package and name, even after it isn't listed there anymore.

    lanx@lanx-1005HA:~/pm$ cat tst_glob.pl $\="\n"; $n=123; $gr=\*n; print *n{PACKAGE}," :: ",*n{NAME}," = ", ${*n{SCALAR}}; delete $main::{n}; print *{$gr}{PACKAGE}," :: ",*{$gr}{NAME}," = ", ${*{$gr}{SCALAR}}; lanx@lanx-1005HA:~/pm$ perl tst_glob.pl main :: n = 123 main :: n = 123

    Cheers Rolf

    PS: Je suis Charlie!

      see Symbol Symbol::delete_package
      # # of Safe.pm lineage # sub delete_package ($) { my $pkg = shift; # expand to full symbol table name if needed unless ($pkg =~ /^main::.*::$/) { $pkg = "main$pkg" if $pkg =~ /^::/; $pkg = "main::$pkg" unless $pkg =~ /^main::/; $pkg .= '::' unless $pkg =~ /::$/; } my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/; my $stem_symtab = *{$stem}{HASH}; return unless defined $stem_symtab and exists $stem_symtab->{$leaf +}; # free all the symbols in the package my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; foreach my $name (keys %$leaf_symtab) { undef *{$pkg . $name}; } # delete the symbol table %$leaf_symtab = (); delete $stem_symtab->{$leaf}; }
        See what? I have no idea what the pasted code is supposed to prove.

        Please take advantage of preview before posting code.

        BTW: see also Re^2: dumping lexical filehandles (not in STASH), showing that the slots PACKAGE and NAME are sometimes wrongly set in Perl (lexicals have no package)

        Cheers Rolf

        PS: Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found