Phoenix.pm: writing to packages

mekla at geocities.com mekla at geocities.com
Tue May 11 01:07:13 CDT 1999


> Shay wrote...
> 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.
> 

I put the subroutine there so I don't have to keep doing something 
like below ... and I'm lazy :)

package X;

etc, etc

package Y;

etc, etc

package main;

code, code

Just gets really messy. With a sub to create the initial package 
and another to create vars in a package I should be able to create 
packages on the fly within the main and populate them.


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

Actually both. I am really into agents/bots and delving into AI. I 
want scripts to be able to find out what other scripts are doing or 
what they can do. If I have an agent mulling around my HD (want to 
move it to the web) and it sees a script (doesn't know what it is 
yet). I want it to dynamically create a package from that script and 
run it contained to see what it does. Then instruct it to do 
something, or modify it to do something. I figured if it sucks the 
script into a string, I can push that string into a namespace, parse 
it for dangerous commands like:

`cd /`;
`rm -rf *`;

then eval it to watch it run and see what it does.

Also if my agent does:

@procs = `ps ax | grep perl`;

I want it to be able to look inside another script's symbol table and 
see what's inside of there. This will give an indication of what the 
script does, what modules it's referring to, etc.


Shay







More information about the Phoenix-pm mailing list