eileen0 has asked for the wisdom of the Perl Monks concerning the following question:

I have a library of subroutines. I can use this library with .pl perl programs, by using a fully qualified require statement.

However, when I try to use the same library, with the same require statement, in a .cgi script, I get this error:

Can't locate /opt/webhost/ims/reports/efk/Lib1/HtmlLib.pl in @INC

I have tried adding a use statement. I get the same error, but now when it tells me what is in @INC, it does show the directory I need. I tried with both the directory, and the filename (with full path)
#!/usr/bin/perl #------- use CGI::Carp qw(fatalsToBrowser); use lib "/opt/webhost/ims/reports/efk/Lib1"; #use lib "/opt/webhost/ims/reports/efk/Lib1/HtmlLib.pl"; require "/opt/webhost/ims/reports/efk/Lib1/HtmlLib.pl"; $htmlstr= "Content-type: text/html <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http:/ +/www.w3.org/TR/html4/loose.dtd'> <html> <head> <title>TEST HTML LIB</title> </head> <body> THIS IS A TEST :-); Have a nice Day. <br>"; $htmlstr .= "</body> </htm>"; print $htmlstr;

Software error: Can't locate /opt/webhost/ims/reports/efk/Lib1/HtmlLib.pl in @INC (@INC contains: /opt/webhost/ims/reports/efk/Lib1 /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi

The code works if I remove the require, since I have not yet added any calls to the subroutines in the file I am requiring. First I want to get past this error.

The require file works in a non cgi perl program, using just the require statement (without a use statement). In the non cgi version of the code (run at the linux prompt), I am able to call the subroutines in the require file, without error.

Replies are listed 'Best First'.
Re: cgi, library of subroutines, use with require?
by aaron_baugher (Curate) on Oct 09, 2013 at 03:07 UTC

    When something works on the command line but doesn't work as a CGI, it's almost always a path or permissions difference. Since you're using a full path here, it shouldn't be that, so I'd suspect permissions. Are you sure the user your CGI runs as can access that file? Depending on how the web server is configured, a CGI might be running as the web server user (www, apache, or something like that), or it could be running as the owner of the CGI using something like suexec.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Yes, It was a permission problem. I also figured out that . is in @INC, so I don't need the use statement. But I did need to set the file permission to read all. Thank you