| 1 | package BabelObjects::Util::Dvlpt::Log; | ||
| 2 | |||
| 3 | use Carp; | ||
| 4 | use strict; | ||
| 5 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | require AutoLoader; | ||
| 9 | |||
| 10 | @ISA = qw(Exporter AutoLoader); | ||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 13 | # Do not simply export all your public functions/methods/constants. | ||
| 14 | @EXPORT = qw( | ||
| 15 | ); | ||
| 16 | $VERSION = '1.00'; | ||
| 17 | |||
| 18 | my %fields = ( | ||
| 19 | file => 'STDERR', | ||
| 20 | debug => 1 | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $proto = shift; | ||
| 25 | my $args = shift; | ||
| 26 | |||
| 27 | my $class = ref($proto) || $proto; | ||
| 28 | |||
| 29 | my $self = { | ||
| 30 | _permitted => \%fields, | ||
| 31 | %fields, | ||
| 32 | }; | ||
| 33 | |||
| 34 | bless ($self, $class); | ||
| 35 | |||
| 36 | if ($args) { | ||
| 37 | my %parameters = %$args; | ||
| 38 | foreach (keys %parameters) { | ||
| 39 | # the following lines are useful to verify argument values | ||
| 40 | #print STDERR "$_ = ", $parameters{$_}, "\n"; | ||
| 41 | #print STDERR "$_ = ", $self->$_, "\n"; | ||
| 42 | $self->$_($parameters{$_}); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub log { | ||
| 50 | my $self = shift; | ||
| 51 | my $msg = shift; | ||
| 52 | |||
| 53 | if ($self->debug == 1) { | ||
| 54 | print STDERR $msg."\n"; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub AUTOLOAD { | ||
| 59 | my $self = shift; | ||
| 60 | my $type = ref($self) or croak "$self is not an object"; | ||
| 61 | |||
| 62 | my $name = $AUTOLOAD; | ||
| 63 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 64 | |||
| 65 | unless (exists $self->{_permitted}->{$name} ) { | ||
| 66 | croak "Can't access `$name' field in class $type"; | ||
| 67 | } | ||
| 68 | |||
| 69 | if (@_) { | ||
| 70 | return $self->{$name} = shift; | ||
| 71 | } else { | ||
| 72 | return $self->{$name}; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | __END__ |