Hi Monks,
I have this small Perl Module to trap warning error(s) to a text file, but it sometimes instead of creating like it supposed to a text file into the current directory where the program is running from, it is creating the text file to the root directory of the program calling it. I don't even know where to start looking for this bug, may be there is something on my module that should be done differently n order not to produce surprises like that.
any help or thoughts on that?
Thanks and here is a sample of this module:

package Log::Error; use 5.008004; use strict; use Cwd; use CGI::Carp qw(carpout); use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use vars qw(@EXPORT @ISA $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw( a_log e_log ); $VERSION = '0.01'; my $localtime = localtime; our $dir; BEGIN { if ($::dir) { $dir = $::dir; } else { $0 =~ '(.*[\\\/])\w+\.\w+$'; $dir = $1; } } # Set up main error log file my $log_file = "e_log.txt"; #Croak will get the line number of the calling program, not the librar +y itself. open(LOG, ">>$dir/$log_file") or croak "Unable to append to error log: + $!"; carpout(*LOG); ##### ***subroutine who print in Appplication Specific Error*** sub a_log{ my $a_localtime = localtime; my ($a_log, @data) = @_; #To determine the name of the currently running function, including it +s package by using the built-in function "caller". my $sub_al = (caller(0))[3]; #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($a_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi) { $a_log="a_log.txt" } open(LOGAPP, ">>$dir$a_log") or croak "Unable to append to $a_log: $!"; #print LOGAPP "[$localtime] $a_log : @data ::::: $!\n"; print LOGAPP "[$a_localtime] $a_log : @data \n"; close LOGAPP or die "Cannot close log file:: $a_log : $!\n"; } sub e_log{ my $e_localtime = localtime; my ($e_log, @e_data) = @_; #To determine the name of the currently running function, including it +s package by using the built-in function "caller". my $sub_el = (caller(0))[3]; #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($e_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi) { $e_log="errorapp_log.txt" } open(ERRAPPLOG, ">>$dir$e_log") or croak "Unable to append to $e_log: $!"; print ERRAPPLOG "[$e_localtime] $e_log : @e_data\n"; close ERRAPPLOG or die "Cannot close log file:: $e_log : $!\n"; } 1; __END__ =head1 DESCRIPTION Errors trapped by -w (Warning) will issue a warning and report the lin +e number of the error. Die will cause the program to die and report the line number of the er +ror. All the errors will be printed to e_log.txt in the same directory wher +e the program that caused the error is running. Called like: &e_log('my_file.txt','Oh no, $hit hit the fan.');

2006-05-03 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Module Question'


In reply to Logging module sometimes writes its file in the wrong directory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.