Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

symboltable problem

by morgon (Deacon)
on Jun 20, 2012 at 16:53 UTC ( #977406=perlquestion: print w/ replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I would like to understand why the following script does not behave as expected.

In the script I had expected that $Bubba::abba would become an alias for $Hubba::abba, but that does not seem to be the case - so evidently %Bubba:: = %Hubba:: is not doing what I thought it would do....

Can someone explain the behaviour to me?

use strict; use Data::Dumper; # put something in package "Hubba" $Hubba::abba = "zappa\n"; # assign Hubba-symtable to Bubba-symtable %Bubba:: = %Hubba::; # verify that they are the same print Dumper(\%Hubba::); print Dumper(\%Bubba::); # use 2 ways to print $abba from Hubba print ${*Hubba::abba{SCALAR}}; # works print $Hubba::abba; # works print "and now Bubba:\n"; print ${*Bubba::abba{SCALAR}}; # prints nothing print $Bubba::abba; # print nothing

Comment on symboltable problem
Download Code
Re: symboltable problem
by Anonymous Monk on Jun 20, 2012 at 17:31 UTC

    Its called magic :) There is a reason modules on cpan for this, see alias , namespace, and stash

    use Data::Dump; $FOO::ONE = 1; *BAR:: = *FOO::; $BAR::ONE = 2; dd \%BAR::, \%FOO::; dd \*BAR::; __END__ do { my $a = { ONE => *FOO::ONE }; $a->{ONE} = \1; ($a, $a); } do { my $a = \*main::FOO::; *{$a} = { ONE => *FOO::ONE }; *{$a}{HASH}{ONE} = \1; $a; }

    use Data::Dump::Streamer; $FOO::ONE = 1; *BAR:: = *FOO::; $BAR::ONE = 2; Dump \%BAR::, \%FOO::; Dump \*BAR::; __END__ $HASH1 = { ONE => *FOO::ONE }; *FOO::ONE = \do { my $v = 1 }; $HASH2 = $HASH1; $GLOB1 = \*{'::FOO::'}; *{'::FOO::'} = { ONE => *FOO::ONE }; *FOO::ONE = \do { my $v = 1 };
      My problem is not to learn how to achieve an alias but to understand the behavour of the script supplied.
Re: symboltable problem
by Anonymous Monk on Jun 20, 2012 at 18:00 UTC

    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
      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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977406]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2015-05-29 05:43 GMT
Find Nodes?
    Voting Booth?

    In my home, the TV remote control is ...

    Results (575 votes), past polls