package Logger; $VERSION = 1.00; use strict; use vars qw( $AUTOLOAD ); use Carp; { #Encapsulated class data my %_log_attr = # DEFAULT ACCESSIBILITY ( _name => ['DEFAULT', 'read/write'], _handle => [undef, 'read'] ); #is a specirfied object attribute accessible in a given mode sub _accessible { my ($self, $attr, $mode) = @_; $_log_attr{$attr}[1] =~ /$mode/ } # Classwide default value for a specified object attributes sub _default_for { my ($self, $attr) = @_; $_log_attr{$attr}[2]; } # list of names of all specified object attributes sub _standard_keys { keys %_log_attr; } } sub new { my ($caller, %arg) = @_; my $caller_is_obj = ref($caller); my $class = $caller_is_obj || $caller; my $self = bless {}, $class; foreach my $attrname ($self->_standard_keys() ) { my ($argname) = ($attrname =~ /^_(.*)/); if (exists $arg{$argname}) { $self->{$attrname} = $arg{$argname} ; } elsif ($caller_is_obj) { $self->{$attrname} = $caller->{$argname}; } else { $self->{$attrname} = $self->_default_for($attrname) } } $self->{_handle}=&prepare_file($self); return $self; } sub prepare_file { my $self = shift; if ($self->{_name}) { my @time=localtime; ++$time[4]; if (length($time[3])eq 1 ) {$time[3]='0'.$time[3]}; if (length($time[4])eq 1 ) {$time[4]='0'.$time[4]}; $time[5]=$time[5]+1900; my $name = '>>'.$self->{_name}.".".$time[3].$time[4].$time[5].'.log'; local*LOGFILE; open(LOGFILE, $name) || undef; return *LOGFILE; } } sub log { use Data::Dumper; #print Dumper(@_); my ($self, $location, $message) = @_; if ($self->{_handle}) { my $fh = $self->{_handle}; print $fh "$location, $message. \n"; } else { print "Must call logger->new(name=>'') before writing log"; } } 1