Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I like the idea of having Objects as accessors. It struck me that traits could be of use here. Here are two traits: The Getter:
package Class::Trait::TGetter; use strict; use warnings; use Class::Trait 'base'; sub get { my $self = shift; return $self->{object}->{$self->{property}}; } 1;
and the Setter:
package Class::Trait::TSetter; use strict; use warnings; use Carp; our $VERSION = '0.03'; use Class::Trait 'base'; #this doesnt work as expected #our %OVERLOADS = ( '=' => "set" ); sub set { my $self = shift; my $value = shift; if ( my $sub = $self->{validate} ) { if ( &$sub ($value) ) { #print "assign $value to $self->{property}\n"; $self->{object}->{$self->{property}} = $value; } else { croak "Illegal value assigned for property $self->{propert +y}"; } } else { #print "assign $value to $self->{property}\n"; $self->{object}->{$self->{property}} = $value; } } 1;
These are be "consumed" by a GetterSetter (There could also be a pure Getter and a pure Setter) class:
package GetterSetter; use strict; use warnings; use Class::Trait ( 'Class::Trait::TGetter' => {}, "Class::Trait::TSetter" => {} ); sub new { my $class = shift; my $self = {object=>shift, property=>shift, validate=>shift}; return bless $self, $class; } 1;
This class can be used by other classes as accessor:
package Person; use warnings; use strict; use GetterSetter; sub new { my $class = shift; my %args = @_; my $self = bless {}, $class; $self->age->set ( $args{age} || 0 ); $self->name->set ( $args{name} || "" ); return $self; } sub age { my $self = shift; # return GetterSetter with validation return GetterSetter->new($self, "age", sub { return $_[0] =~ /^[0- +9]+$/ }); } sub name { my $self = shift; # return GetterSetter without validation return GetterSetter->new($self, "name");; }
Now the Person class can be used
use Person; eval { my $p = Person->new(name => "holli", age => "30"); print $p->name->get, ", ", $p->age->get, "\n"; }; eval { my $p = Person->new(); $p->name->set("holli"); $p->age->set(30); print $p->name->get, ", ", $p->age->get, "\n"; }; #this croaks "Can't modify non-lvalue subroutine call at..." #but shouldn't because the setter should be overloaded #Can you jump in [Ovid]? eval { my $p = Person->new(); $p->name = "holli"; $p->age = 30; print $p->name->get, ", ", $p->age->get, "\n"; }; print $@; #croaks "Illegal value assigned for property age..." eval { my $p = Person->new(name => "Santa Claus", age => "unknown"); print $p->name->get, ", ", $p->age->get, "\n"; }; print $@; #croaks "Illegal value assigned for property age..." eval { my $p = Person->new(); $p->name->set("Santa Claus"); $p->age->set ("unknown"); print $p->name->get, ", ", $p->age->get, "\n"; }; print $@;
Thoughts?


holli, /regexed monk/

In reply to accessor traits (Re: The Accessor Heresy) by holli
in thread The Accessor Heresy by Roy Johnson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found