modperl_dynamic_package_names

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: modperl@apache.org
From: Andrew Maltsev <am@amsoft.ru>
Subject: Dynamic package name
Date: Fri, 9 Mar 2001 14:05:49 -0800 (PST)

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?

===

To: Andrew Maltsev <am@amsoft.ru>
From: Andrew Ho <andrew@tellme.com>
Subject: Re: [OT] Dynamic package name
Date: Fri, 9 Mar 2001 16:52:37 -0800 (PST)

Andrew,

AM>Is there any way to tell perl that from now on the namespace is
AM>$namespace? `package' does not accept scalars, and its scope is
AM>eval block, so if I use eval to pass scalar to "package" I have to
AM>include 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

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu