in reply to "Fields" for "Objects"
package Person; use Carp; our $AUTOLOAD; # it's a package global my %fields = ( name => undef, age => undef, peers => undef, ); sub new { my $class = shift; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; }
Which is basically going to "auto build" anything that's set in your permitted hash. I've used this before and it's saved a great deal of time (and tediousness).sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { croak "Can't access `$name' field in class $type"; } if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: "Fields" for "Objects"
by stvn (Monsignor) on Jun 10, 2009 at 19:45 UTC | |
by shmem (Chancellor) on Jun 11, 2009 at 17:41 UTC | |
by stvn (Monsignor) on Jun 12, 2009 at 00:39 UTC | |
by shmem (Chancellor) on Jun 12, 2009 at 01:07 UTC | |
by dekimsey (Sexton) on Jun 11, 2009 at 02:26 UTC | |
Re^2: "Fields" for "Objects"
by lodin (Hermit) on Jun 10, 2009 at 21:02 UTC |