You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ken Clarke <ke...@shaw.ca> on 2005/05/27 03:30:27 UTC

Accessing main:: from within a package

Hi Folks,

    With the release of mod_perl 2 (all say "Yeah Stas!" :) I decided to 
dive in.  It'll be a change of mindset for me since I've coded perl scripts 
for mod_cgi for years.

    I have a library of perl modules which occasionally make reference to 
package main:: (EG $main::ScriptGlobalHash{'hashkey'} or my $result = 
&main::SomeSubroutine(\%args))  However, if my understanding of the docs is 
correct, my "main" script now belongs to a request URL specific package 
namespace.  How should I properly (read safely/portably) refer to my "main" 
package?  Put another way, how should my packages refer to variables, 
subroutines, handles etc which belong to the "top level script".

    Your time is appreciated.

>> Ken



Re: Accessing main:: from within a package

Posted by Stas Bekman <st...@stason.org>.
Ken Clarke wrote:
> Hi Folks,
> 
>    With the release of mod_perl 2 (all say "Yeah Stas!" :) I decided to 
> dive in.  It'll be a change of mindset for me since I've coded perl 
> scripts for mod_cgi for years.
> 
>    I have a library of perl modules which occasionally make reference to 
> package main:: (EG $main::ScriptGlobalHash{'hashkey'} or my $result = 
> &main::SomeSubroutine(\%args))  However, if my understanding of the docs 
> is correct, my "main" script now belongs to a request URL specific 
> package namespace.  How should I properly (read safely/portably) refer 
> to my "main" package?  Put another way, how should my packages refer to 
> variables, subroutines, handles etc which belong to the "top level script".

Ken, I suppose you are talking about registry scripts.

To move your code into the main:: package (which as you've noticed 
ModPerl::RegistryCooker moves into a unique-per-script namespace), all you 
need to do is declare it at the top of your script. So if you had:

   #!/usr/bin/perl
   print "Content-type: text/html\n\n";
   print "mod_perl is easy";

now it'll look like:

   #!/usr/bin/perl
   package main;
   print "Content-type: text/html\n\n";
   print "mod_perl is easy";

I haven't tried that, but I think it should do the trick.

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Re: Accessing main:: from within a package

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2005-05-26 at 18:30 -0700, Ken Clarke wrote:
>     I have a library of perl modules which occasionally make reference to 
> package main:: (EG $main::ScriptGlobalHash{'hashkey'} or my $result = 
> &main::SomeSubroutine(\%args))  However, if my understanding of the docs is 
> correct, my "main" script now belongs to a request URL specific package 
> namespace.  How should I properly (read safely/portably) refer to my "main" 
> package?  Put another way, how should my packages refer to variables, 
> subroutines, handles etc which belong to the "top level script".

Ideally you wouldn't do it that way.  If you have modules that need
values from the calling script, you should either pass them to the subs
you call, create a module with subs that you can use to access them, or
export them from a module which the different parts of your script can
share.  The last is closest to what you're doing now.  Take a look at
the examples of sharing config data here:
http://perl.apache.org/docs/1.0/guide/porting.html#Configuration_Files__Writing__Dynamically_Updating_and_Reloading

- Perrin