Gossamer Forum
Home : General : Perl Programming :

module include question...

Quote Reply
module include question...
If I create a module (let's call it Test.pm), put it in the GT folder, then reference it in my cgi using:

use GT::Test;

How do I reference a variable I have set within Test.pm? Let's say I have a variable called $testvar that is set in the Test.pm module, and I want to reference it in my cgi script. What is the syntax for pulling the variable into my cgi script?

(I'm new to perl... Can you tell? Laugh)

Sean

Last edited by:

SeanP: May 9, 2002, 5:58 PM
Quote Reply
Re: [SeanP] module include question... In reply to
Try this

$GT::Test:testvar

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [SeanP] module include question... In reply to
You have to package the module via the following codes:

Quote:

package GT::Test;


At the top of the Test.pm file.

Then you can reference the appropriate sub like the example posted by tandat, like:

Quote:

&sub({something => $something});


since you've already specified that you are using the PM module in your script, it is not necessary to use the complete OO reference.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] module include question... In reply to
Here's my global:

Code:
sub {
my $tags = shift;
my $query = $IN->param('query');
my $stopwords = $GT::SQL::Search::Base::Common:$STOPWORDS;

if (length $query < 3) {
return "Your query is to short.";
}
if (exists $stopwords{$query}) {
return "The word '$query' is a stopword.";
}
return $tags->{error}; # Display default error
}

I can't seem to figure out the syntax... The $STOPWORDS variable in Common.pm isn't within a sub, so I'm not sure what to put for the &sub({something => $something}); example you gave me...

Sean
Quote Reply
Re: [SeanP] module include question... In reply to
According to the code $STOPWORDS is exported by default so you should be able to just require GT::SQL::Search::Base::Common and then you can use $STOPWORDS without doing anything.
Quote Reply
Re: [Paul] module include question... In reply to
I tried:

Code:
sub {
use GT::SQL::Search::Base::Common;
my $tags = shift;
my $query = $IN->param('query');

if (length $query < 3) {
return "Your query is to short.";
}
if (exists $STOPWORDS{$query}) {
return "The word '$query' is a stopword.";
}
return $tags->{error}; # Display default error
}

But I get:

GT::Config (6105): Unable to compile 'error_formatted' in file '/www/cgi-bin/admin/templates/default/globals.txt': Global symbol "%STOPWORDS" requires explicit package name at (eval 248) line 9.
at /www/cgi-bin/admin/GT/Template.pm line 461.
Quote Reply
Re: [SeanP] module include question... In reply to
$STOPWORDS is a hashref not a normal hash.

Use "require" instead of "use" then change:

if (exists $STOPWORDS{$query}) {

to

if (exists $STOPWORDS->{$query}) {
Quote Reply
Re: [Paul] module include question... In reply to
Thanks! That's works!!! Smile

Sean
Quote Reply
Re: [Paul] module include question... In reply to
I may have spoke too soon. I'm getting this error again:

Code:
GT::Config (6227): Unable to compile 'error_formatted' in file '/www/cgi-bin/admin/templates/default/globals.txt': Global symbol "$STOPWORDS" requires explicit package name at (eval 247) line 6.
at /www/cgi-bin/admin/GT/Template.pm line 461.

I can't tell if it's just a cache problem or not. I have restarted my web server, cleared my cache, and the error pops up after I add the global. When I removed the global, it goes away. Here's the final one I'm using:

Code:
sub {
require GT::SQL::Search::Base::Common;
my $tags = shift;
my $query = $IN->param('query');

if (exists $STOPWORDS->{$query}) {
return "The word '$query' is too common.";
}
if (length $query < 2) {
return "Your query is too short.";
}
return $tags->{error}; # Display default error
}

Am I missing something?

Sean
Quote Reply
Re: [SeanP] module include question... In reply to
Hmm I thought that should have worked.

Under:

require GT::SQL::Search::Base::Common;

Try adding:

my $STOPWORDS = $GT::SQL::Search::Base::Common::STOPWORDS;
Quote Reply
Re: [Paul] module include question... In reply to
It looks like that did it. Wink

It was kinda weird... I got it to work once, but was getting the error every now and then. Being that I use mod_perl, I decided to restart the web server. After that, I got the error everytime I hit the site.

Sean