Note the following code:

#! C:/perl/bin/perl -w
# AmsLog.pm 05/24/2004.

package AmsLog;

# Declare pragmas.
use diagnostics;
use strict;
use warnings;
use constant DEBUG => 0;

# Declare modules.
use FindBin qw($Bin);
use lib $Bin;
use Date::Format;
use Fcntl qw(:DEFAULT :flock);
use FileHandle;
use Net::SMTP;
use Win32::Message;

# Declare global variables.
my ($LogFile, $Smtp);

#--------------------------------------------------------------------------#
# Object constructor for this class.                                       #
#--------------------------------------------------------------------------#
sub new()
{
    my $Proto = shift;                 # Class name argument.
    my $Class = ref($Proto) || $Proto;
    my $LogName = shift;               # File indentifier.
    my $Self  = {Class => $Class, @_}; # Add the remaining arguments to the
class hash.
    $ENV{DOMAIN} = 'nisc.cc';          # Specify the domain-name for SMTP.
    $Smtp = Net::SMTP->new('xxx.xxx.xxx');
    LogOpen($Self,$LogName);           # Define and open the log file.
    return bless($Self,$Class);
}

#--------------------------------------------------------------------------#
# Object destructor for this class.                                        #
#--------------------------------------------------------------------------#
sub DESTROY() {LogClose(shift); $Smtp->quit if (defined $Smtp)}

#--------------------------------------------------------------------------#
# Provide a method to define and open the log file.                        #
#--------------------------------------------------------------------------#
sub LogOpen()
{
    my $Self    = shift; # Class name.
    my $LogName = shift; # File indentifier.
    my $Str;

    # Determine the pathname depending upon the machine name.
    if ($ENV{COMPUTERNAME} =~ /(mail|sol)/i) {$LogFile = 'C:/Logs/'}
    else {$LogFile = $ENV{TEMP}; $LogFile =~ s^\\^/^g; $LogFile .= '/'}

    # Define the log filename.
    $LogFile =
join('',$LogFile,time2str('%Y_%m_%d',time),'_',$LogName,'.log');

    # If the log filename exists, open it for appending, else open it for
output.
    if (-e $LogFile) {$Str = ">>$LogFile"}
    else             {$Str = ">$LogFile"}

    # Create a filehandle reference that will be unique to each instance of
    # this object and ad it to the object's hash.
    $Self->{FH} = FileHandle->new();

    # Open the log file.
    die("Unable to open $LogFile $!, stopped") unless
(open($Self->{FH},$Str));
    WriteLog($Self,"$Self->{Class}: $LogFile has been opened",0) if DEBUG;

    # If requested, lock the log file.
    if (exists($Self->{Lock}) and $Self->{Lock} == 1)
    {
        die("*** $LogFile cannot be locked ***") unless (flock($Self->{FH},
LOCK_EX | LOCK_NB));
        WriteLog("$LogFile has been locked",0) if DEBUG;
    }

    WriteLog('SMTP object constructor call failed.',1) unless (defined
$Smtp);

    return(1);
}

#--------------------------------------------------------------------------#
# Provide a method to close the log file.                                  #
#--------------------------------------------------------------------------#
sub LogClose()
{
    my $Self = shift; # Class name.
    my $FH   = $Self->{FH};
    WriteLog($Self,"$Self->{Class}: Closing $LogFile",0) if DEBUG;
    die("Close failed for filehandle $FH") unless (close($FH));
    return(1);
}

I am getting an error when the DESTROY sub invokes the LogClose sub. The
error is as follows:

(in cleanup) Uncaught exception from user code:
Can't use an undefined value as a symbol reference at
C:/Perl/Scripts/AmsLog.pm line 92 during global destruction.
AmsLog::LogClose('AmsLog=HASH(0x1ab51c8)') called at
C:/Perl/Scripts/AmsLog.pm line 42
AmsLog::DESTROY('AmsLog=HASH(0x1ab51c8)') called at
C:\Perl\Scripts\MovePDF.pl line 0
eval {...} called at C:\Perl\Scripts\MovePDF.pl line 0

Line 92 is the actual close function call in the LogClose sub. I infer that
the $Self->{FH} is undefined at the point it is referenced in LogClose. This
is interesting as the 3rd edition of the Camel book, page 330, has a code
example very similar to what I am attempting to do. I realize it is probably
not neccessary to close the file and the most simple solution is not to
close the file. But on the other hand, I feel that something like this
should work. What am I doing wrong?

On a similar note, how could I display all of the key/value pairs in the
object hash?

Dirk Bremer - Systems Programmer II - ESS/AMS  - NISC St. Peters
USA Central Time Zone
636-922-9158 ext. 8652 fax 636-447-4471

[EMAIL PROTECTED]
www.nisc.cc

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to