LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   problem with perl modules declaration and perl/cgi script (http://www.linuxquestions.org/questions/programming-9/problem-with-perl-modules-declaration-and-perl-cgi-script-791004/)

shifter 02-23-2010 09:38 AM

problem with perl modules declaration and perl/cgi script
 
I have Ubuntu 9.10 with Apache 2.2.x and perl 5.10.0.
I writed littles perl modules in my home directory.

I tried to lanch a following perl/cgi:

Quote:

#!/usr/bin/perl

use MyModule;


my $cgi = CGI->new();

my $mProject = new MyModule;


$mProject->create;
I have .html files in /var/www directory and perl/cgi script in /usr/lib/cgi-bin directory but MyModule is in my home directory.

Firefox output 500 Internal Server Error.

/var/log/apache2/error.log file is:
Quote:

[Tue Feb 23 15:28:22 2010] [error] [client ::1] Premature end of script headers: createproj.cgi, referer: http://localhost/projects.html
Can't locate MyModule.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at /usr/lib/cgi-bin/createproj.cgi line 3.
BEGIN failed--compilation aborted at /usr/lib/cgi-bin/createproj.cgi line 3.
[Tue Feb 23 15:39:27 2010] [error] [client ::1] Premature end of script headers: createproj.cgi, referer: http://localhost/projects.html

What is the problem?

theNbomr 02-23-2010 10:02 AM

The perl interpreter cannot locate your package 'MyModule.pm'.
Code:

#!/usr/bin/perl
use lib "/your/home/dir";
use MyModule;

my $cgi = CGI->new();

my $mProject = new MyModule;

$mProject->create;

--- rod.

shifter 02-23-2010 10:45 AM

Ok, now it is an other error.

/var/log/apache2/error.log file is:
Quote:

Can't locate MyModule.pm in @INC (@INC contains: /my/home/dir/ /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at /usr/lib/cgi-bin/createproj.cgi line 4.
BEGIN failed--compilation aborted at /usr/lib/cgi-bin/createproj.cgi line 4.
[Tue Feb 23 16:45:28 2010] [error] [client ::1] Premature end of script headers: createproj.cgi, referer: http://localhost/projects.html

My createproj.cgi script is:
Quote:

#!/usr/bin/perl

use lib "/my/home/dir/";
use MyModule;
use CGI;


my $cgi = CGI->new();

my $pname = $cgi->param('projname');

my $mProject = new MyModule;

$mProject->create($pname);
What is this other problem?

theNbomr 02-23-2010 12:06 PM

Does the error occur when you run the script from the commandline and with $PWD of /usr/lib/cgi-bin? And, what I should have said right at the start:
Code:

#! /usr/bin/perl -w
use strict;

It may be that the web server has no access to your home directory, and cannot load files from there.

--- rod.

shifter 02-23-2010 02:30 PM

The output in /var/log/apache2/error.log file is:
Quote:

Can't locate MyModule.pm in @INC (@INC contains: /my/home/directory/ /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at /usr/lib/cgi-bin/createproj.cgi line 5.
BEGIN failed--compilation aborted at /usr/lib/cgi-bin/createproj.cgi line 5.
[Tue Feb 23 20:24:22 2010] [error] [client 127.0.0.1] Premature end of script headers: createproj.cgi, referer: http://localhost/projects.html
The /usr/lib/cgi-bin/createproj.cgi script is:
Quote:

#!/usr/bin/perl -w
use strict;

use lib "/my/home/directory/";
use MyModule;
use CGI;


my $cgi = CGI->new();

print $cgi->header();

my $pname = $cgi->param('projname');

print 'pname = '.$pname."\n";

my $mProject = new MyModule;

$mProject->create($pname);
myname@desktop:/home$ ls -lh
totale 4,0K
drwxr-xr-x 54 myname myname 4,0K 2010-02-23 20:24 myname
myname@desktop:/home$

Firefox output "500 Internal Server Error".

What is the problem?

theNbomr 02-23-2010 04:08 PM

First, you said
Quote:

I have .html files in /var/www directory and perl/cgi script in /usr/lib/cgi-bin directory but MyModule is in my home directory.
But then you show
Quote:

myname@desktop:/home$ ls -lh
totale 4,0K
drwxr-xr-x 54 myname myname 4,0K 2010-02-23 20:24 myname
So, where exactly is MyModule.pm?
Put that directory in the 'use lib' directive, and you should get past the problem.
--- rod.

shifter 02-23-2010 05:02 PM

I added to createproj.cgi

Quote:

use lib '/home/myname/MyModule.pm'
now works, ok thank you

shifter 02-23-2010 06:40 PM

No, excuse me, I mistaked, I didn't solve my problem.

1)
In Firefox appears: 500 Internal Server Error

2)
/var/log/apache2/error.log file is:
Quote:

Can't locate MyModule.pm in @INC (@INC contains: /home/savio /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at /usr/lib/cgi-bin/myscript.cgi line 5.
BEGIN failed--compilation aborted at /usr/lib/cgi-bin/myscript.cgi line 5.
[Wed Feb 24 00:45:39 2010] [error] [client 127.0.0.1] Premature end of script headers: myscript.cgi, referer: http://localhost/projects.html
3)
My cgi script is:
Quote:

#!/usr/bin/perl -w
use strict;

use lib '/home/savio/';
use MyModule;
use CGI;
use CGI::Carp qw(fatalsToBrowser);


my $cgi = CGI->new();

my $pname = $cgi->param('projname');
my $var = new MyModule;
$var->create($pname);
with MyModule.pm in /home/savio.

What is the problem?

Thanks in advance for answers,
savio

theNbomr 02-24-2010 12:36 AM

If the script runs cleanly from the commandline, but not when run by the web server, then your web server must not have access to your $HOME tree. Try putting the module in one of the standard places used by Perl, and see if that fixes it. You may be able to grant the user under which the web server runs group read-execute permissions, and then make that user a member of your home group, evidently 'savio'.
Is this host using SElinux? If so try disabling that, at least as a test. I don't know what needs to be done to poke a hole in it if that turns out to be the problem.

Just to be clear, we are talking about directories on the server, right?

--- rod.

theNbomr 02-24-2010 10:09 AM

Okay, I just did a test of what you are trying to do.
In the server cgi-bin directory:
Code:

4.0K -rwxr--r-- 1 apache root    434 Feb 24 06:55 modTest.pl
In a directory in my $HOME tree:
Code:

4.0K -rwxr--r-- 1 theNbomr theNbomr  251 Feb 24 06:55 MyMod.pm
In /etc/group (note: restart web server after adding apache to user group)
Code:

theNbomr:x:1234:theNbomr,apache
modTest.pl
Code:

#! /bin/perl -w
use strict;
use CGI;
use lib "/home/theNbomr/Util/pl";
use MyMod;

    my $page = CGI->new();
    my $x = MyMod->new();
    print $page -> header,                    # create the HTTP header
          $page -> start_html('hello world'), # start the HTML
          $page -> h1('hello world');        # level 1 header
    print $x -> tod();                        # print time-of-day
    print $page -> end_html;                  # end the HTML

    exit;

MyMod.pm
Code:

package MyMod;
use strict;

sub new(){
my $proto = shift;
my $class = ref( $proto ) || $proto;
my $self = {};
    bless $self, $class;
    return $self;
}

sub tod(){
my $self = shift;
    return "ToD : ", my $time = localtime(),"\n";
}
1;

The code works without errors. I think adding the apache user to the theNbomr user group and then restarting the server is the key. Your server may run as a different user.

--- rod.


All times are GMT -5. The time now is 11:33 PM.