Home: Perl Programming Help: mod_perl:
What package is the main script in?



kencl
User

Aug 16, 2003, 5:04 PM


Views: 20474
What package is the main script in?

Hi Folks,

Does anyone know what the package name is for a script running under Apache::Registry? I'm trying to access a subroutine in a custom perl module I've written.

In simple terms, here's what I'm doing:

Main Script
our %G;
$G{'dirs'}{'template'} = '/home/username/safe/templates/';
use Custom::Common; # contains sub Error
use Custom::Template; # contains sub Load_Template

my %args = (template => 'template_filename.html');
my $template;
unless($template = &Load_Template(\%args)) {
# print error message here
}

package Custom::Template
sub Load_Template {
my $arg_ref = shift;

# test access to template file
my $file = $main::G{'dirs'}{'template'} . $arg_ref->{'template'};
unless (-e $file) {
&main::Error({ msg => "Load_Template: requested template file ($file) does not exist.",
actions => 'collect_all log_msg email_developer' });
return 0;
}
}


What's happening is that the reference to main:: isn't correct, so the path to $file is not correct. Since the path is not correct, the -e file test returns false. The call to &main::Error is unable to find sub Error and leaves this message in the error log: "Undefined subroutine &main::Error called at /home/username/safe/pm/Custom/Template.pm line 32"

sub Error is exported into package main properly, and this system works fine when run as a CGI script.

I'm guessing that the problem is the reference to main::, when in fact it's probably the Apache handler subroutine which is in main and my "main" script is in some other package. Any suggestions? Thanks.



>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

(This post was edited by kencl on Aug 16, 2003, 5:50 PM)


Paul
Enthusiast

Aug 20, 2003, 5:29 PM


Views: 20463
Re: [kencl] What package is the main script in?

It's not really a mod_perl issue, it's basic modulation issues.

You aren't specifying the class name. Instead of:

Load_Template(\%blah) you need:

Custom::Template::Load_Template(\%blah)

...then it will find the right routine. You only leave the class name out if you are importing the routine into your script, eg...

use Custom::Template qw/Load_Template/;


(This post was edited by Paul on Aug 20, 2003, 5:30 PM)


kencl
User

Aug 21, 2003, 2:24 PM


Views: 20457
Re: [Paul] What package is the main script in?

sub Load_Template is imported into the "main" script properly via use Custom::Template. It doesn't need to be explicitely imported as it is part of the default export list.

I have determined the name of the script's package. It is:
Apache::ROOTwww_2edomain_2enet::cgi_2dbin::dev::test_2empl::handler

Since this contains the script name, I cannot use it within my packages. A new strategy for creating a global variable which is accessable within imported packages is required.

I'll keep working on it and post my result here for the benefit of others who may be interested in how to accomplish this.


>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


kencl
User

Dec 16, 2006, 2:32 PM


Views: 16072
Re: [kencl] What package is the main script in?

My solution was to explicitely name the package of my "main" script.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!