开发者

Environment variable for perl cgi scripts

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.


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';


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
    ;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜