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

I want to split up my code in different files, by defining own modules. The output should be on a website. While the script, when executed on the console prints the right output (with no error), it gives following error in the browser:

The specified CGI application encountered an error and the server terminated the process.

Both files are in the same directory, i use windows-active state perl and i want to work with packages.

file hello-2.pl

#!d:\perl\bin\perl.exe use warnings; use strict; use Hello; #this line causes the error print"Content-type:text/html;charset=UTF-8\n\n" ; Hello->hello();

file Hello.pm

#!d:\perl\bin\perl.exe use warnings; use strict; package Hello; use Exporter; my @ISA = ('Exporter'); my @EXPORT = ('hello'); sub hello { print "Hello, world\n"; } 1;

Executed on the console it prints "hello, world". In the browser there is "The specified CGI application encountered an error and the server terminated the process." unless I remove the line "use Hello". So if I would delete the include of the hello package, it would work. What is wrong with "use Hello"?

Replies are listed 'Best First'.
Re: cgi and own modules causing unspecified error
by Corion (Patriarch) on Jun 08, 2017 at 14:07 UTC

    Have you looked at your web server error log? It has more information.

    In which directory does Hello.pm live?

    The current directory of your program when it is run from your web server is not what you think it is.

    The easiest approach is to tell Perl where to find its files:

    use lib 'c:/users/amitsq/whatever/path/to/own/modules';

      yes that's it. Expanding my example now, i get another error "Can't locate object method "new" via package "Hello" (perhaps you forgot to load "Hello"?)"


      thats the new code:
      Hello-2.pl
      #!d:\perl\bin\perl.exe use lib 'S:\incubator\ex\Hello.pm'; print"Content-type:text/html;charset=UTF-8\n\n" ; my $obj = Hello->new( sample_data => 'hello world', more_data => 'blah blah blah' ); $obj->sample_method();
      Hello.pm
      #!d:\perl\bin\perl.exe package Hello; #SampleObject; sub new { my ($class, %args) = @_; return bless { %args }, $class; } sub sample_method { my ($self) = @_; print $self->{sample_data}; } 1;

        Hey amitsq, the following is incorrect:

        use lib 'S:\incubator\ex\Hello.pm';

        You add the *path* to the use lib pragma, not the file:

        use lib 'S:\incubator\ex';
        use lib 'S:\incubator\ex\Hello.pm';

        Add the directory in which modules reside, rather than the full path to the module. Also you still need to use it:

        use strict; use warnings; use lib '/home/marto/temp'; use Hello; my $obj = Hello->new( sample_data => 'hello world', more_data => 'blah blah blah' ); $obj->sample_method();

        Also: lib.

        Can't locate object method "new" via package "Hello" (perhaps you forgot to load "Hello"?)

        The hint is right - you did forget to load "Hello". See use.

Re: cgi and own modules causing unspecified error
by clueless newbie (Curate) on Jun 08, 2017 at 18:13 UTC
    I'd be inclined to add a fatalsToBrowser call: For example something like
    #!/usr/bin/env perl use CGI::Carp qw(fatalsToBrowser); use strict; use warning; exit;
    yields
    Status: 500 Content-type: text/html <h1>Software error:</h1> <pre>Can't locate warning.pm in @INC (you may need to install the warn +ing module) (@INC contains: C:/Perl/perl/site/lib C:/Perl/perl /vendor/lib C:/Perl/perl/lib .) at cgi_01.pl line 5. BEGIN failed--compilation aborted at cgi_01.pl line 5. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p> [Thu Jun 8 13:07:59 2017] cgi_01.pl: Can't locate warning.pm in @INC +(you may need to install the warning module) (@INC contains: C:/Perl/perl/site/lib C:/Perl/perl/vendor/lib C:/Perl/perl/lib .) at c +gi_01.pl line 5. [Thu Jun 8 13:07:59 2017] ncgi_01.pl: BEGIN failed--compilation abort +ed at cgi_01.pl line 5.
    Remember to remove the "use CGI::Carp qw(fatalsToBrowser);" before putting your code into production.
Re: cgi and own modules causing unspecified error
by stevieb (Canon) on Jun 08, 2017 at 14:25 UTC

    Like Corion said, the actual error will be logged in the web server's error log. As far as the file location of Hello.pm, if it's simply in the same directory in the script, it's most likely that the file can't be found.

    When you run the script on the command line from within the directory the script and module files are, everything will work. However, the web server executes the script from a different directory than that, which would result in Hello.pm not being found.

    Either put the module file into a path that's in @INC, or do what was suggested and use lib ...;