in reply to Error Module not working in Browser, Help!

A module is intended to be a library of routines but not main programs. The code after the BEGIN block and before 'sub app_error' is anonymous and therefore only runs when the module is run (incorrectly) as a main program. To run that code from another script it needs to be placed inside a method like sub new { your code } and called by the name of the new method.

Everything but the troll

Replies are listed 'Best First'.
Re^2: Error Module not working in Browser, Help!
by Errto (Vicar) on Jan 09, 2006 at 18:01 UTC

    This is not correct. That code is executed when the module is loaded for the first time (in a CGI environment, it will presumably be loaded each time a CGI script runs because each one is a separate Perl process). See use and require as well as the "Perl Modules" section of perlmod.

    The OP did not tell us what he/she means in saying the module doesn't work, but my guess is that it is not writing the log or writing it to the wrong place. Fundamentally, the rule of thumb I'd suggest is don't use cwd() in CGI programs. Instead, explicitly set the directory for the log file, either hard-coded or stored in a confirguration file in a fixed location.

      That's exactly what is going on, it can't print to and create the log file(s). If I hard code the path it will defeat the propose of the code cause I need the module to generate log specific for each script running. But why do you think that it does its job from one place but the browser?

        I don't quite see what you're getting at, but it sounds like you're assuming that cwd() will return the directory that the CGI script is located in. This is not the case. The value of cwd() is dependent on your web server configuration, and you cannot depend on it when calling a script through CGI.

        If each script should have its own log file, then provide a method in your module that takes an argument specifying the location of the log file, and each script can then specify its own.

Re^2: Error Module not working in Browser, Help!
by Anonymous Monk on Jan 09, 2006 at 16:37 UTC
    Could you give an example?