Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: symboltable problem

by morgon (Curate)
on Jun 20, 2012 at 19:22 UTC ( #977452=note: print w/replies, xml ) Need Help??

in reply to Re: symboltable problem
in thread symboltable problem

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"?

Replies are listed 'Best First'.
Re^3: symboltable problem
by Eliya (Vicar) on Jun 20, 2012 at 21:26 UTC

    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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://977452]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-02-24 05:21 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.