in reply to OOP's setter/getter method - is there a way to avoid them?
If i remember this code is from the old but still worth reading book Perl CookBook. In this book an entire chapter is dedicated to Object but starting from the very primitive concepts (the book is aged).package Person; use strict; use Carp; use vars qw($AUTOLOAD %ok_field); # Authorize four attribute fields for my $attr ( qw(name age peers parent) ) { $ok_field{$attr}++; } sub AUTOLOAD { my $self = shift; my $attr = $AUTOLOAD; $attr =~ s/.*:://; return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap metho +ds croak "invalid attribute method: -> $attr()" unless $ok_field{$attr}; $self->{uc $attr} = shift if @_; return $self->{uc $attr}; } sub new { my $proto = shift; my $class = ref($proto) || $proto; my $parent = ref($proto) && $proto; my $self = {}; bless($self, $class); $self->parent($parent); return $self; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: OOP's setter/getter method - is there a way to avoid them?
by tiny_monk (Sexton) on Oct 27, 2015 at 09:44 UTC | |
by Preceptor (Deacon) on Oct 27, 2015 at 10:37 UTC | |
by hippo (Archbishop) on Oct 27, 2015 at 10:08 UTC |