Closed p5pRT closed 13 years ago
I have a package with a very\, very long name and many varibles that I want to address within the package. I'd like to alias the entire namespace. I recall doing something like this in older Perls:
$foo::bar::poit::var="hlaghlag"; *g::=\%foo::bar::poit::; # Alias g:: to some long thing. print $g::var; # Should print hlaghlag
Someone else remembered being able to do this as well\, so I don't think I'm completely senile. It doesn't work now. Explantion:
Anyways\, in the debugger I can dump g's symbol table and it shows $var is there. And I can print $g::var at a debugger prompt\, and it's there. Actually trying to use $g::var in the body of the program yeilds undef.
main::(foo.pl:4): $foo::bar::poit::var="hlag"; DB\<1> n main::(foo.pl:5): *g::=\%foo::bar::poit::; DB\<1> n main::(foo.pl:6): print $g::var\, "\n"; DB\<1> V g $var = 'hlag' # It's there! DB\<2> print $g::var hlag # No\, really! It's there! DB\<3> n # \<--- hlag should have printed\, It's gone! Debugged program terminated. Use q to quit or R to restart\, use O inhibit_exit to avoid stopping after program termination\, h q\, h R or h O to get additional info.
This is under perl 5.6 and perl5.005. At the very least\, it's confusing and not very DWIM'ish.
Same result on 5.12.0.
On Thu Jan 18 04:09:13 2001\, clintp wrote:
I have a package with a very\, very long name and many varibles that I want to address within the package. I'd like to alias the entire namespace. I recall doing something like this in older Perls:
$foo::bar::poit::var="hlaghlag"; *g::=\%foo::bar::poit::; # Alias g:: to some long thing. print $g::var; # Should print hlaghlag
Someone else remembered being able to do this as well\, so I don't think I'm completely senile. It doesn't work now. Explanation:
Anyways\, in the debugger I can dump g's symbol table and it shows $var is there. And I can print $g::var at a debugger prompt\, and it's there. Actually trying to use $g::var in the body of the program yields undef.
main::(foo.pl:4): $foo::bar::poit::var="hlag"; DB\<1> n main::(foo.pl:5): *g::=\%foo::bar::poit::; DB\<1> n main::(foo.pl:6): print $g::var\, "\n"; DB\<1> V g $var = 'hlag' # It's there! DB\<2> print $g::var hlag # No\, really! It's there! DB\<3> n # \<--- hlag should have printed\, It's gone! Debugged program terminated. Use q to quit or R to restart\, use O inhibit_exit to avoid stopping after program termination\, h q\, h R or h O to get additional info.
This is under perl 5.6 and perl5.005. At the very least\, it's confusing and not very DWIM'ish.
-- Alexandr Ciornii\, http://chorny.net
Same result on 5.12.0.
On Thu Jan 18 04:09:13 2001\, clintp wrote:
I have a package with a very\, very long name and many varibles that I want to address within the package. I'd like to alias the entire namespace. I recall doing something like this in older Perls:
$foo::bar::poit::var="hlaghlag"; *g::=\%foo::bar::poit::; # Alias g:: to some long thing. print $g::var; # Should print hlaghlag
Someone else remembered being able to do this as well\, so I don't think I'm completely senile. It doesn't work now. Explanation:
Anyways\, in the debugger I can dump g's symbol table and it shows $var is there. And I can print $g::var at a debugger prompt\, and it's there. Actually trying to use $g::var in the body of the program yields undef.
main::(foo.pl:4): $foo::bar::poit::var="hlag"; DB\<1> n main::(foo.pl:5): *g::=\%foo::bar::poit::; DB\<1> n main::(foo.pl:6): print $g::var\, "\n"; DB\<1> V g $var = 'hlag' # It's there! DB\<2> print $g::var hlag # No\, really! It's there! DB\<3> n # \<--- hlag should have printed\, It's gone! Debugged program terminated. Use q to quit or R to restart\, use O inhibit_exit to avoid stopping after program termination\, h q\, h R or h O to get additional info.
This is under perl 5.6 and perl5.005. At the very least\, it's confusing and not very DWIM'ish.
-- Alexandr Ciornii\, http://chorny.net
It has nothing to do with the debugger. Anything that delays compilation until after the aliasing is done will suffice.
Apparently\, the "$g::var" in "print $g::var" is resolved at compile-time.
----- BEGIN CODE ----- $foo::bar::poit::var = "fbp"; $g::var = "g"; BEGIN { $h::var = "h"; }
*g:: = \%foo::bar::poit::; # Alias g:: to some long thing. BEGIN { *h:: = \%foo::bar::poit::; # Alias h:: to some long thing. }
print "$foo::bar::poit::var\n"; # Prints fbp print "$g::var\n"; # Prints g print "$h::var\n"; # Prints fbp
eval ' print "$foo::bar::poit::var\n"; # Prints fbp print "$g::var\n";; # Prints fbp print "$h::var\n";; # Prints fbp '; ----- END CODE -----
I suspect that fixing this problem will incur a performance penalty.
There are three workarounds:
1) Perform the aliasing sooner (e.g. at compile-time).
2) Use symbolic references instead to access the variables (forcing run-time name resolution).
3) Delay the compilation of the variable access (e.g. by using eval).
----- BEGIN CODE ----- $foo::bar::poit::var = "fbp"; $g::var = "g"; BEGIN { $h::var = "h"; }
*g:: = \%foo::bar::poit::; # Alias g:: to some long thing. BEGIN { *h:: = \%foo::bar::poit::; # Alias h:: to some long thing. }
# 1) Do aliasing sooner print "$foo::bar::poit::var\n"; # Prints fbp print "$g::var\n"; # Prints g print "$h::var\n"; # Prints fbp
# 2) Lookup variable later. { no strict 'refs'; print "${'foo::bar::poit::var'}\n"; # Prints fbp print "${'g::var'}\n"; # Prints fbp print "${'h::var'}\n"; # Prints fbp }
# 3) Compile variable access later eval ' print "$foo::bar::poit::var\n"; # Prints fbp print "$g::var\n"; # Prints fbp print "$h::var\n"; # Prints fbp '; ----- END CODE -----
It’s not possible to fix this\, as it would break compatibility in all sorts of edge cases.
@cpansprout - Status changed from 'open' to 'rejected'
Migrated from rt.perl.org#5249 (status was 'rejected')
Searchable as RT5249$