Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problems with 'require'

0 views
Skip to first unread message

Pawel Predki

unread,
Nov 28, 2009, 8:04:54 PM11/28/09
to beginn...@perl.org
Hey,
I've been using simple CGI scripts to make some things on my website
require less human touch and some of them require mysql database
connection. I've started with only one such scripts but now there are
more so I've decided to move the connection data (table name, database
name, username, password) to a separate .pm file which I could include
in the other .cgi scripts. However, I don't seem to be able to use the
functions and/or variables in this .pm module and I have no idea what is
wrong. Let me give you a simple example of what works and what doesn't
and I would really appreciate any help.

<file mydbtest.pm>
package dbredwings;

my $platform = "mysql";
my $database = "dbplayers";
my $host = "localhost";
my $tablename = "players";
my $user = "player";
my $pw = "pass";

sub printout {
print shift;
}

sub get_platform {
print $platform;
}
1;
</file mydbtest.pm>

<file properscript.cgi>
require 'mydbtest.pm';
print "Content-type: text/html\n\n";
&mydbtest::printout('hey hey'); # (1)
&mydbtest::get_platform; # (2)
print $mydbtest::platform; # (3)
</file properscript.cgi>

(1) works without any problems. I get the proper output in my browser
window.
(2) doesn't work at all. I get an error as follows:
Undefined subroutine &mydbtest::get_platform called at
E:/webdev/perl/properscript.cgi line 42.
(3) prints an empty string

Also, I get an error message saying that the .pm file can't be found
when I place it in the same directory as the .cgi files. I have to move
it to a directory in the @INC array when I run it through the server.
However, when I run it locally, from the command line, everything works
just fine. This makes me think that there is something in the Apache
config that I have to do in order for all this to work but I don't know
what that is.

I'm running ActivePerl 5.10.1 with Apache2.2 with mod_perl.
Alias /perl "E:\webdev\perl"
<Location /perl>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options +ExecCGI
PerlOptions +ParseHeaders
</Location>
ScriptAlias /cgi-bin/ "E:/webdev/perl/cgi-bin/"

Cheers,
palo

Pawel Predki

unread,
Nov 29, 2009, 1:02:24 PM11/29/09
to Matt Whipple, beginn...@perl.org

Matt Whipple pisze:
> Paweł Prędki wrote:
>> <file mydbtest.pm>
>> package dbredwings;
> For organization reasons, you should probably make the package name
> match the file name


>> my $platform = "mysql";
>> my $database = "dbplayers";
>> my $host = "localhost";
>> my $tablename = "players";
>> my $user = "player";
>> my $pw = "pass";
>>
>> sub printout {
>> print shift;
>> }
>>
>> sub get_platform {
>> print $platform;
>> }
>> 1;
>> </file mydbtest.pm>
>>
>> <file properscript.cgi>
>> require 'mydbtest.pm';
>> print "Content-type: text/html\n\n";
>> &mydbtest::printout('hey hey'); # (1)

> A package is a logical grouping within code, and it is through packages
> which you would specify functions. Once a file is loaded (here through
> require) into memory, the original filename becomes somewhat
> inconsequential. These functions should therefore be
> dbredwings::printout, etc. (here also dropping the & which is considered
> somewhat deprecated for simple function calling). This is the reason
> for the previous comment about package naming.
The package name and the file name are the same on my machine. I simply
forgot to correct it when writing my last e-mail. I'm using the proper
syntax, that is packagename::functionname;
>
> I'd suggest reading the perlmod perldoc section a bit more, or cut out
> packaging all together if you're not taking advantage of it and treat
> the code as one big scary scope.

I will read up on that, thanks.

>> &mydbtest::get_platform; # (2)
>> print $mydbtest::platform; # (3)
>> </file properscript.cgi>
>>
>> (1) works without any problems. I get the proper output in my browser
>> window.

> Dumb luck... use strict; use warnings; ?

Got both in the file now.

>> (2) doesn't work at all. I get an error as follows:
>> Undefined subroutine &mydbtest::get_platform called at
>> E:/webdev/perl/properscript.cgi line 42.
>> (3) prints an empty string
>>
>> Also, I get an error message saying that the .pm file can't be found
>> when I place it in the same directory as the .cgi files. I have to
>> move it to a directory in the @INC array when I run it through the
>> server. However, when I run it locally, from the command line,
>> everything works just fine. This makes me think that there is
>> something in the Apache config that I have to do in order for all this
>> to work but I don't know what that is.

> The require statement access the file directly (outside the Web server),
> so it wouldn't be Apache's configuration. It could be a permissions
> problem however.

I changed all the permissions and what I get now is that everything
works fine but whenever I make changes to the .pm file I need to restart
the server that is running locally for the changes to be seen in my main
script... This is not the case on the live target server where I use
both the .pm file and the scripts. So, after all, everything is sort of
OK right now.

Mike Williams

unread,
Nov 29, 2009, 6:04:08 PM11/29/09
to beginn...@perl.org
On Sun, Nov 29, 2009 at 1:02 PM, Paweł Prędki <
pawel....@detroitredwings.pl> wrote:

>
>
> I changed all the permissions and what I get now is that everything works
> fine but whenever I make changes to the .pm file I need to restart the
> server that is running locally for the changes to be seen in my main
> script... This is not the case on the live target server where I use both
> the .pm file and the scripts. So, after all, everything is sort of OK right
> now.
>
>
>
>> I'm running ActivePerl 5.10.1 with Apache2.2 with mod_perl.
>>>
>>

Apache::Reload will resolve the issue of having to restart the server.

I don't know if it's in the Active State repository, if not, its available
on CPAN
http://search.cpan.org/search?query=Apache::Reload&mode=module

Mike

Matt Whipple

unread,
Nov 29, 2009, 7:26:47 AM11/29/09
to Paweł Prędki, beginn...@perl.org
Paweł Prędki wrote:
> <file mydbtest.pm>
> package dbredwings;
For organization reasons, you should probably make the package name
match the file name
>
> my $platform = "mysql";
> my $database = "dbplayers";
> my $host = "localhost";
> my $tablename = "players";
> my $user = "player";
> my $pw = "pass";
>
> sub printout {
> print shift;
> }
>
> sub get_platform {
> print $platform;
> }
> 1;
> </file mydbtest.pm>
>
> <file properscript.cgi>
> require 'mydbtest.pm';
> print "Content-type: text/html\n\n";
> &mydbtest::printout('hey hey'); # (1)
A package is a logical grouping within code, and it is through packages
which you would specify functions. Once a file is loaded (here through
require) into memory, the original filename becomes somewhat
inconsequential. These functions should therefore be
dbredwings::printout, etc. (here also dropping the & which is considered
somewhat deprecated for simple function calling). This is the reason
for the previous comment about package naming.

I'd suggest reading the perlmod perldoc section a bit more, or cut out


packaging all together if you're not taking advantage of it and treat
the code as one big scary scope.

> &mydbtest::get_platform; # (2)
> print $mydbtest::platform; # (3)
> </file properscript.cgi>
>
> (1) works without any problems. I get the proper output in my browser
> window.

Dumb luck... use strict; use warnings; ?

> (2) doesn't work at all. I get an error as follows:
> Undefined subroutine &mydbtest::get_platform called at
> E:/webdev/perl/properscript.cgi line 42.
> (3) prints an empty string
>
> Also, I get an error message saying that the .pm file can't be found
> when I place it in the same directory as the .cgi files. I have to
> move it to a directory in the @INC array when I run it through the
> server. However, when I run it locally, from the command line,
> everything works just fine. This makes me think that there is
> something in the Apache config that I have to do in order for all this
> to work but I don't know what that is.

The require statement access the file directly (outside the Web server),
so it wouldn't be Apache's configuration. It could be a permissions
problem however.

>

Matt Whipple

unread,
Nov 29, 2009, 9:16:56 AM11/29/09
to Paweł Prędki, beginn...@perl.org
Paweł Prędki wrote:
>
> I changed all the permissions and what I get now is that everything
> works fine but whenever I make changes to the .pm file I need to
> restart the server that is running locally for the changes to be seen
> in my main script... This is not the case on the live target server
> where I use both the .pm file and the scripts. So, after all,
> everything is sort of OK right now.
>
That's a result of mod_perl loading/compiling Perl modules on server
startup. There may be a development mode flag to change this behavior
with Apache::Registry, otherwise look at Apache::PerlRun for testing.
0 new messages