1

I have a cgi script similar to the following:

BEGIN {
    unshift (@INC, "$ENV{'HOME'}/www/cgi-bin/SiteSpecific");
}

print "Content-type: text/html\n\n";
use SiteObject;
my $siteObjInst = SiteObject->instance();
print $siteObjInst->{HideFields};

This would run fine from the command prompt, but fails when run as a CGI script from a browser. The $ENV{'HOME'} is perhaps not set as the script cannot find the module.

Is it that the CGI scripts are not run within a shell and do not find the environment variables?

If the above is true, do I need to set the desired variables within the BEGIN block using some other means?

Thanks for your help.

Axeman
  • 29,660
  • 2
  • 47
  • 102
rpat
  • 279
  • 2
  • 5
  • 12

3 Answers3

4

A CGI program will have its environment set by the web server. HOME may or not be set depending on how your web server is set up, and if it is set it will probably point to the home directory of the user that the web server is running as, not your home directory.

You can print the value of $ENV{HOME} from the CGI program, or even better, print the entire %ENV hash to see what's really happening.

In my experience its better to either hardcode the full path to extra libraries or set the path externally (eg using PERL5LIB). If you're setting it from inside the program, use the "lib" pragma rather than modifying @INC directly:

use lib '/home/user/www/cgi-bin/siteSpecific';
Rob N
  • 1,015
  • 8
  • 20
1

You definitely can't guarantee that your shell and the id running the web server have the same variable for $ENV{HOME}. Forget all of that code for a little while and try this:

print "Content-type: text/html\n\n";
print  q[<html><head><title>A Page</title></head>]
    . qq[<body><h1>\$HOME=$ENV{HOME}</h1></body></html>]
    ;

Or even this:

use strict;
use warnings;
use CGI;

my $q = CGI->new;
print $q->header
    , $q->start_html( 'A Page' )
    , $q->start_table
    , $q->Tr( $q->th( 'Name' ), $q->th( 'Value' ))
    , ( map { $q->Tr( $q->td( $_ ), $q->td( $ENV{$_} )) } sort keys %ENV )
    , $q->end_table
    , $q->end_html
    ;
Axeman
  • 29,660
  • 2
  • 47
  • 102
  • I am already learning a lot from your answers. However, I am trying to deal with a specific problem. – rpat Dec 16 '10 at 06:41
  • trying to deal with a specific problem. I have several websites on a server. I have code that is common to all the sites except one module which is specific to each domain. The common code make use of the specific – rpat Dec 16 '10 at 06:48
  • @rpat, none of that changes that in order to know if your pushing the right directory into `@INC`, you need to know where the server process thinks "HOME" is--using the method above. – Axeman Dec 16 '10 at 06:55
  • I realize that the I cannot use the ENV part. I have several websites (on one server) which use common code. However, the common code in turn makes use of a module(SiteSpecific.pm) that is customized for each site. The common code needs use include path based on the specific website to pick up the right SiteSpecific.pm. So the include path would vary. I was wondering if I can use some means to set the include path in the BEGIN BLOCK. Unfortunately, I cannot edit the web server files. – rpat Dec 16 '10 at 07:26
  • 1
    @rpat, you *should* be able to `use lib` or `unshift` paths into `@INC`. The problem seems to be that you're not inserting a *good* path into the list, not that you can't adjust the include path in a `BEGIN` block--because you *can*--other people on my team do it a little too much for my tastes. – Axeman Dec 16 '10 at 07:43
1

For a list of some issues and potential answers, I'd start with How can I troubleshoot my Perl CGI script?.

Community
  • 1
  • 1
David Harris
  • 2,332
  • 1
  • 13
  • 25