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

Hi Monks!
I am trying to create a module to use in all me Perl programs, but I can't get it to work, can someone take a look at it and give some advice and also how it should properly be called in my scripts.
Thanks a lot!
package NewError; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; @ISA = qw(Exporter AutoLoader); # 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. @EXPORT = qw(log_error std_log_error); #$VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /: (\d+)\.(\d+)/; $VERSION = '0.01'; # Define global variables use vars qw( $curr_dir $work_file $log_file $std_log_file); #--------------------------------------------------------------------- +------------ # Grab the current directory from $0. The following regular expressio +n # looks for 'CURR_DIR\xxxxx.xxx' or 'CURR_DIR/xxxxx.xxx'. Place it in # the BEGIN block so we can use it to include modules #--------------------------------------------------------------------- +------------ BEGIN { $0 =~ '(.*[\\\/])\w+\.\w+$'; $curr_dir = $1 || "./"; } sub log_error{ # Set up error log file $log_file = "error_log.txt"; use CGI::Carp qw(carpout); open(LOG, ">>${curr_dir}${log_file}") or die "Unable to append to error log: $!"; carpout(*LOG); } ##### End Sub log_error sub std_log_error{ #This will log general errors generated as example by user interaction my @data = @_; $std_log_file = "error_log_std.txt"; my ( $year, $mon, $day, $hr, $min, $sec) = Today_and_Now(); my $timestamp = sprintf "%02d-%02d-%02d:%02d:%02d:%02d ", $mon, $day, +($year - 2000), $hr, $min, $sec; open(LOG_FILE, ">>${curr_dir}${std_log_file}") or die "Unable to append to error log: $!"; print LOG_FILE $timestamp, @data, "\n"; close( LOG_FILE ); } #### End Sub std_log_error 1; __END__

Replies are listed 'Best First'.
Re: Module Help
by holli (Abbot) on Dec 30, 2005 at 17:29 UTC
    but I can't get it to work,
    is one of my favourite error description. What do you mean by it? Do you get an error message, does it show unexpected behavior?

    Also, this looks like a logging module. There are plenty of them on cpan, starting from Log::Simple to Log4Perl.


    holli, /regexed monk/
      I can't make the code in spec write to the files in the code.
Re:Module Help
by tinita (Parson) on Dec 30, 2005 at 20:04 UTC
    require Exporter; @ISA = qw(Exporter AutoLoader);
    why do you use AutoLoader as a base class?
    use base 'Exporter'; # short for require and @ISA
Re: Module Help
by choedebeck (Beadle) on Dec 30, 2005 at 21:55 UTC
    First, There are many logging modules out there, my favorite of which is Log::Log4perl. It would probably be easier if you just used one of these instead of writing your own.

    Assuming this is not an option, there is a module called Cwd that comes standard with perl that will get you the current working directory. This will keep you from having to parse the directory out of $0. You might also want to turn warnings on, I found found that with warnings, it is much easier to find errors than without them.

    Also it looks like you are trying to redirect the file error_log.txt to the httpd error log, but I am not seeing anywhere were error_log.txt is getting written to. Hope this helps.
Re: Module Help
by InfiniteSilence (Curate) on Dec 30, 2005 at 18:26 UTC
    What is your $0 returning? On my ActiveState Perl on Win32 it only returns the name of the current file. See here for more details:
    $0 Contains the name of the file containing the Perl script being + executed. Depending on your OS, it may or may not include the full p +athname.

    Celebrate Intellectual Diversity

      It returns the current directory.