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

I have a question, I need help here where I created this module to use in all my Perl scripts. It can be used first by
use ErrorModule;
and if I need to get a specific error for a particular script, I can call this subroutine “ app_error” inside the module like this:
&app_error("test.txt","File Name that created this error: test_mod.pl" +);

What I can’t understand is, why this module only works from the command prompt and not when I run the scripts from the browser. The Module is installed in c:/Perl/lib/
Please someone since I can get any answer anywhere.
Thanks a lot!!!!
Here is the module installed in c:/Perl/lib/
#!C:\perl\bin\perl.exe package ErrorModule; # The code in the BEGIN block will be executed very early # on, even before the rest of this script is parsed. BEGIN { # Use the CGI::Carp module and import the carpout() function. use CGI::Carp qw(carpout); #Send warnings and die messages to the browser. carpout(STDOUT); } use 5.008004; use strict; use warnings; #use CGI::Carp qw(carpout); use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use ErrorMod ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT = qw( app_error ); our $VERSION = '0.01'; our $localtime = localtime; #The Cwd module retrives the current working directory. #The safest way to get the current working directory is with #the cwd() function. This funstion is guaranteed to work on ALL #operating systems and should be used in most cases. use Cwd; our $dir = cwd(); # Set up error log file my $log_file = "error_log.txt"; open(LOG, ">>$dir/$log_file") or die "Unable to append to error log: $ +!"; carpout(*LOG); ##### ***subroutine who print in Appplication Specific Error*** sub app_error{ my $log_app = $_[0]; my @data = $_[1]; #Make sure that the filename doesn't have spaces or extensions like ex +e or bat on it, #otherwise it will create file as app_log.txt instead. unless($log_app=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi) { $log_app="app_log.txt" } open(LOGAPP, ">>$dir/$log_app") or die "Unable to append to $log_app: $!"; print LOGAPP "[$localtime] $log_app : @data $!\n"; carpout(*LOGAPP); close LOGAPP or die "Cannot close log file:: $log_app : $!\n"; } 1; __END__


And here is a test script
#!/perl/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw(-oldstyle_urls :standard); use ErrorModule; my $empty; # case 1 my $test = "test"; print header(); print "<br> This is a test<br>"; print $test; print $empty; print "<form action=madetofail.pl method=get>"; print "<input type=text><input type=submit value=go>"; &app_error("test.txt","File Name that created this error: test_mod.pl" +); print "after" # left ; out to fail and to log error by ErrorModule

Tks, again!

Replies are listed 'Best First'.
Re: Error Module not working in Browser, Help!
by DungeonKeeper (Novice) on Jan 09, 2006 at 16:21 UTC
    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

      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?
      Could you give an example?
Re: Error Module not working in Browser, Help!
by DungeonKeeper (Novice) on Jan 10, 2006 at 09:42 UTC
    The scope of the file handles might be the problem for the failing situation. Perhaps:
    use strict; use warnings;
    will reveal a number of issues. I tend to use "open my $fh" rather than hard-coded handles. I also tend to store the generated file handles in a dictionary hash rather than rely on being able to sort scoping problems out.

    Everything but the troll