&app_error("test.txt","File Name that created this error: test_mod.pl");
####
#!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 export
# 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 @EXPORT_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 exe 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__
####
#!/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 "
This is a test
";
print $test;
print $empty;
print "