in reply to symboltable problem

Here is some revelation

print 2, because *BAR::ONE (SCALAR slot included ) is aliased to *FOO::ONE

perl -e" $FOO::ONE = 1; *BAR::ONE = *FOO::ONE ; $BAR::ONE = 2; print +$FOO::ONE "

prints 1 because *BAR::ONE slots isn't aliased to *FOO::ONE slots

$ perl -e" $FOO::ONE = 1; *BAR:: = *FOO:: ; $BAR::ONE = 2; print $FOO +::ONE 1

Replies are listed 'Best First'.
Re^2: symboltable problem
by morgon (Priest) on Jun 20, 2012 at 19:22 UTC
    Ok, call me stupid but I don't get what you are trying to say.

    I re-phrase my question:

    For package "Hubba", it's symbol table is stored in a hash %Hubba:: that maps symbol-names to typeglobs.

    So when I do %Bubba:: = %Hubba:: (I am NOT doing *Bubba::=*Hubba::) the whole symbol-to-typeglob mapping is copied to another hash (which happens to be the symbol-table of another package).

    So why does that then not create aliases?

    I can do this:

    our $var = "zappa"; $Bubba::{abba} = *var; print $Bubba::abba;
    which shows that I can create variables by manipulating the symbol-table. Why does that not happen in my script?

    Can someone explain that in plain english without using the word "magic"?

      I think the problem is that %Bubba:: = %Hubba:: is creating a new hash, which destroys the binding of $Bubba::abba to the original hash set up at compile time of the latter expression.  This reasoning is based on the observation that both of the following work fine:

      #!/usr/bin/perl use strict; $Hubba::abba = "zappa\n"; %Bubba:: = %Hubba::; # compile time deferred eval 'print $Bubba::abba';
      #!/usr/bin/perl use strict; $Hubba::abba = "zappa\n"; # copying via hash slice doesn't create new hash @Bubba::{keys %Hubba::} = @Hubba::{keys %Hubba::}; # or: @Bubba::{keys %Hubba::} = values %Hubba::; print $Bubba::abba;
        Yes, that seems to eplain it. Another solution then is to create the alias in a BEGIN block.
        Thanks - that is very interesting. I did not expect that...

        I find this illustration quite amusing:

        use strict; our $foo = "old value\n"; %:: = (%::); our $var = "new value\n"; $::{foo} = *var; print $foo; eval q| print $foo |;
        btw: Is there a nicer way to create a typeglob with a given value in the scalar-slot?