amazotron has asked for the wisdom of the Perl Monks concerning the following question:

Hi, all. I ran across some code in "Practical Perl Programming" that will solve several problems for me. I also am aware of the Alias module on CPAN, but wanted to see if I could get this working first. Below is a snippet.
package Foo; use strict; sub Test { my @a = (1,2,3); my $aref = \@a; local (*aa) = $aref; push (@aa, 4); # line 11 print "aa:",join(",",@aa),"\n"; # line 12 } Test ();
Why must I qualify uses of aa as in Foo::aa? If I don't I get the error "Variable @aa not imported at xxx line yyy". By the way, having to qualify all the uses of aa is what I'm trying to avoid.

Replies are listed 'Best First'.
Re: symbol tables and class data members
by stephen (Priest) on May 06, 2002 at 18:32 UTC

    You must qualify @aa because it's a package variable, and under 'use strict' you must qualify all package variables. If something is in the symbol table, it must be fully-qualified under strict unless you've declared it with 'our'. (Or use vars if you're not on the latest Perl.)

    It seems to me that what you want is a way of doing symbol-table hacking with lexical vars, and I'm afraid that that's not possible because lexical vars don't appear in the symbol table.

    I'd suggest declaring your vars with 'our'.

    stephen

      I guess I should explain that the package Foo is functioning as a class and Test as a method of that class. I was trying to find an easy way of converting existing package-oriented code into class-based code. That is, move globals to be data members and not have to struggle rewriting all the uses of those globals.