allyc has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am after a bit of advise with writing scripts that will work with Apache::Registry under mod_perl and the name spaces that it uses.

I am currently creating an application that I want to be able to execute as CGI and also with Apache::Registry but I am having problems accessing the main name space.

I currently have the following :

index.pl use strict; # Add the path to the Module to @INC use lib 'C:/inetpub/wwwroot/Altest/AlsModules'; # Load the Module, and Export has %FormData use AlCommon; # Set the Application Details our %ApplicationDetails; $ApplicationDetails{Name} = 'Test App'; $ApplicationDetails{Version} = '1.0'; $ApplicationDetails{Comments} = 'This is my Test App'; # Display the ApplicationAbout function AlCMSCommon->ApplicationAbout(); # End AlCommon.pm package AlCommon; use strict; use Exporter; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT = qw(AlsTemplateBuilder); our @EXPORT_OK = qw(%FormData); sub ApplicationAbout { print “About Application :\n”; foreach my $AboutItem (qw(Name Version Comments)) { print $AboutItem = $main::ApplicationDetails{$AboutItem}\n”; } }

This works fine when run as CGI, but fails with Apache::Registry. I think the problem is that Apache::Registry gives the main package a new package name and so a different name space. I can use caller to get the name space where the hash is located, but I cant work out how to use that to get access to the the data.

Has any one done anything like this in the past, or have any pointers that might help me?

Thanks! Al

Replies are listed 'Best First'.
Re: Apache::Registry and main package
by edan (Curate) on Oct 30, 2004 at 18:11 UTC

    Avoid accessing variables in other packages from within a module like the plague. Pass the variable that you want to use in the subroutine as an argument to the subroutine. Or if you want to get fancy, instantiate an object. Here's the simple way:

    AlCommon::ApplicationAbout( %ApplicationDetails ); # ... package AlCommon; sub ApplicationAbout { my %details = @_; foreach my $AboutItem (qw(Name Version Comments)) { print "$AboutItem = $details{$AboutItem}\n"; } }
    --
    edan

      Thanks guys. Advice taken! I am very very new to writing modules, and I want to try and make them mod_perl compatible which should be fun.

      I will pass the hash to the function as I normally would however what would be the best way to share a hash between the Module and the main script?

      If I declare it in the module and then return a Hash Ref to the calling script?

      I am guessing the same applies if I want to share a Database Handle between them? Just get one of them to pass a reference across?

      Any advice would be greatly appreciated!

      Al

Re: Apache::Registry and main package
by ikegami (Patriarch) on Oct 30, 2004 at 19:35 UTC
    I agree with edan. It is extemely bad practice for a module to access the globals of the calling script. What happens if a different script uses the module?