Phoenix.pm: writing to packages

Beaves at aol.com Beaves at aol.com
Tue May 11 00:22:37 CDT 1999


Shay wrote...

Last question...


Can you create namespaces dynamically like so:

$var = "A";

sub create_namespace($){
  package $_[0];             # or maybe eval ($string = "package $_[0]");
}

sub write_to_package($$$){
  my ($package,$var_name,$var_value) = @_;
  %$package::$var_name = $var_value;
}
++++++++++++++++=

OK, maybe I'm missing something here, but I from what I read, the answer is 
pretty straightforward.

package main;
$a = 'A';

package Page;  # now refer to the symbol table for "Page
$a = 'PageA'      # assigning $a in the Page symbol table to 'PageA'

package main;
print $a;  # outputs 'A'
print $Page::a;   # outputs 'PageA'
$Page::a = 'newPageA';   # reassigning $a in the symbol table 'Page' 

# here is where I don't quite get your subroutine, because you really don't 
need 
# one.  Just preface your variable assignment or creation with the package 
name.

print $Page::a;  #ouputs 'newPageA'

Since, I consider myself relatively new to the Perl game, I may have missed 
the point of your question entirely, (it may have been more in depth).  If 
that's the case, then please don't take offense with me talking about stuff 
you already may know.

Tim.

p.s. were you asking again about two separate Perl scripts accessing each 
others packages?  Or just one script, and how the packages can talk to one 
another?



More information about the Phoenix-pm mailing list