| 1 | # | ||
| 2 | # $Id: Object.pm,v 0.16 2003/08/07 00:01:59 lawrence Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | package Net::Msmgr::Object; | ||
| 6 | use 5.006; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Carp; | ||
| 11 | our $AUTOLOAD; | ||
| 12 | |||
| 13 | sub _fields { return () } ; | ||
| 14 | |||
| 15 | sub AUTOLOAD | ||
| 16 | { | ||
| 17 | my $self = shift; | ||
| 18 | my $type = ref($self) or croak "$self is not an ojbect"; | ||
| 19 | |||
| 20 | my $name = $AUTOLOAD; | ||
| 21 | $name =~ s/.*://; | ||
| 22 | |||
| 23 | unless (exists$self->{_permitted}->{$name} ) | ||
| 24 | { | ||
| 25 | croak "Cannot access `$name' field in class $type"; | ||
| 26 | } | ||
| 27 | |||
| 28 | if (@_) | ||
| 29 | { | ||
| 30 | return $self->{$name} = shift; | ||
| 31 | } | ||
| 32 | else | ||
| 33 | { | ||
| 34 | return $self->{$name} | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $proto = shift; | ||
| 41 | my $class = ref($proto) || $proto; | ||
| 42 | my $self = bless({}, $class); | ||
| 43 | |||
| 44 | if ($class eq 'Object') | ||
| 45 | { | ||
| 46 | croak "Do not instantiate an object. Derive from it."; | ||
| 47 | } | ||
| 48 | |||
| 49 | my @fields = $self->_fields; | ||
| 50 | while (@fields) | ||
| 51 | { | ||
| 52 | my ($k, $v) = splice(@fields, 0, 2); | ||
| 53 | $self->{_permitted}->{$k} = $v; | ||
| 54 | $self->{$k} = $v; | ||
| 55 | } | ||
| 56 | while (@_) | ||
| 57 | { | ||
| 58 | my ($k, $v) = splice(@_,0,2); | ||
| 59 | $self->$k($v); | ||
| 60 | } | ||
| 61 | return $self; | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | sub DESTROY {}; | ||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | |||
| 70 | # | ||
| 71 | # $Log: Object.pm,v $ | ||
| 72 | # Revision 0.16 2003/08/07 00:01:59 lawrence | ||
| 73 | # Initial Release | ||
| 74 | # | ||
| 75 | # | ||
| 76 | |||
| 77 |