Dynamic package name

2001-03-09 Thread Andrew Maltsev

Hi!

Strictly speaking this has nothing to do with mod_perl, I just got
into that problem writing mod_perl application. I want some module
to decide its namespace at loading time. Obviously it can be solved
with one big eval over entire module text, but this is not an option
in my case - the code have to be just a couple of lines at the top
of a module file.

Is there any way to tell perl that from now on the namespace is
$namespace? `package' does not accept scalars, and its scope is
eval block, so if I use eval to pass scalar to "package" I have to
include entire program text into that eval as well.

Here is an example of what I need:

blahblahblah.pm:

  my $namespace="Blah::Blah::Blah";

  something_that_sets_namespace($namespace);

  sub test ()
  { print "I'm in package: ", __PACKAGE__, "\n";
  }

  1;

Any ideas? Is it possible at all?

Andrew.



Re: [OT] Dynamic package name

2001-03-09 Thread Andrew Ho

Andrew,

AMIs there any way to tell perl that from now on the namespace is
AM$namespace? `package' does not accept scalars, and its scope is
AMeval block, so if I use eval to pass scalar to "package" I have to
AMinclude entire program text into that eval as well.

You can kind of do this via XS, but definitely not from regular Perl
without an eval(). Of course, you shouldn't be shy with eval() in mod_perl
as Apache::Registry, Apache::ASP, and most everything else uses eval()
anyway. Anyway, for doing it from XS something like this works:

void declare_package(pTHX_ SV *sv_name) {
PL_curstash = gv_stashsv(sv_name, TRUE);
sv_setsv(PL_curstname, sv_name);
}

Take a look at the Perl documentation for stashes in perlguts(1):

http://www.perl.com/CPAN/doc/manual/html/pod/perlguts.html#Stashes_and_Globs

Humbly,

Andrew

--
Andrew Ho   http://www.tellme.com/   [EMAIL PROTECTED]
Engineer   [EMAIL PROTECTED]  Voice 650-930-9062
Tellme Networks, Inc.   1-800-555-TELLFax 650-930-9101
--