| 1 | package Object::_Initializer; | ||
| 2 | |||
| 3 | # | ||
| 4 | # revision history | ||
| 5 | # | ||
| 6 | # 2001-09-05 AFD initial release | ||
| 7 | # 2001-09-06 AFD add _get_attribute | ||
| 8 | # 2001-10-08 AFD rename to Object::_Initializer | ||
| 9 | # 2001-10-08 AFD add log_it and dump_log | ||
| 10 | # 2001-10-09 AFD add _timestamp, debug_log | ||
| 11 | # 2001-10-12 AFD return undef from _get_attribute instead of croak for missing... | ||
| 12 | # 2001-10-12 AFD add _timeformat | ||
| 13 | # 2001-10-13 AFD remove space from end of _timestamp | ||
| 14 | # 2002-01-16 AFD avoid redefining object methods in _init... | ||
| 15 | # 2002-03-02 AFD call _defaults in new if can... | ||
| 16 | # | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use vars qw($VERSION); | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | |||
| 23 | use Data::Dumper; | ||
| 24 | |||
| 25 | $VERSION = '0.08'; | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | |||
| 29 | my ($class, %args) = @_; | ||
| 30 | my $self = bless {}, ref($class)||$class; | ||
| 31 | $self->_defaults if $self->can('_defaults'); | ||
| 32 | $self->_init(%args); | ||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _init { | ||
| 37 | |||
| 38 | my ($self, %args) = @_; | ||
| 39 | my $class = ref($self); | ||
| 40 | |||
| 41 | for my $arg (keys %args) { | ||
| 42 | $self->{$arg} = $args{$arg}; | ||
| 43 | next if $self->can($arg); | ||
| 44 | my $name = "$class::$arg"; | ||
| 45 | no strict 'refs'; | ||
| 46 | *{$name} = sub { | ||
| 47 | my $oldval = $_[0]->{$arg}; | ||
| 48 | $_[0]->{$arg} = $_[1] if defined $_[1]; | ||
| 49 | return $oldval; | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | sub _get_attribute { | ||
| 56 | |||
| 57 | my ($self, $attribute, $newvalue) = @_; | ||
| 58 | my $class = ref($self); | ||
| 59 | return undef unless exists $self->{$attribute}; | ||
| 60 | # croak "no such attribute '$attribute' for class '$class'" unless exists $self->{$attribute}; | ||
| 61 | my $oldval = $self->{$attribute}; | ||
| 62 | $self->{$attribute} = $newvalue if defined $newvalue; | ||
| 63 | return $oldval; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub log_it { | ||
| 68 | my ($self,$what) = @_; | ||
| 69 | |||
| 70 | if ($self->trace) { | ||
| 71 | my $logfilename = $self->LogFilename; | ||
| 72 | $what = $self->_timestamp.$what; | ||
| 73 | open(LOG, ">>$logfilename") or croak "$0 can't open LOG ($logfilename) $!"; | ||
| 74 | print LOG $what; | ||
| 75 | close LOG; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub dump_log { | ||
| 80 | my ($self) = @_; | ||
| 81 | $self->log_it( ' '.Data::Dumper->Dump([ $_[0] ], [qw( *self)])."\n"); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub debug_log { | ||
| 85 | my ($self) = @_; | ||
| 86 | my ($savetrace,$savelogfile) = ($self->{'trace'},$self->{'LogFilename'}); | ||
| 87 | $self->_init(trace=>1,LogFilename=>'/data/'.ref($self).'.log'); | ||
| 88 | $self->dump_log; | ||
| 89 | $self->trace($savetrace); | ||
| 90 | $self->LogFilename($savelogfile); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _timestamp { | ||
| 94 | my ($self) = @_; | ||
| 95 | my ($SEC, $MIN, $HOUR, $DAY, $MONTH, $YEAR, $WEEKDAY) = (localtime)[0..6]; | ||
| 96 | return sprintf("%04d/%02d/%02d-%02d:%02d:%02d",$YEAR+1900,$MONTH+1,$DAY,$HOUR,$MIN,$SEC); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _timeformat { | ||
| 100 | my ($self) = @_; | ||
| 101 | my ($SEC, $MIN, $HOUR, $DAY, $MONTH, $YEAR, $WEEKDAY) = (gmtime)[0..6]; | ||
| 102 | my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
| 103 | my @days = qw(Sun Mon Tue Wed Thr Fri Sat); | ||
| 104 | return sprintf("%s, %s %2d %04d at %02d:%02d:%02d UT",$days[$WEEKDAY],$months[$MONTH],$DAY,$YEAR+1900,$HOUR,$MIN,$SEC); | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | __END__ |