Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Should I use Fields, InsideOuts, or Properties?

by hakkr (Chaplain)
on Jul 06, 2005 at 14:11 UTC ( [id://472808]=note: print w/replies, xml ) Need Help??


in reply to Should I use Fields, InsideOuts, or Properties?

Encapsulation of an object means you should never access attributes directly but use accessors and mutators(getters and setters).

One exceedling cool way to do this which I think I can credit to someone from this site(please correct me if not) is with Autoload.

If you put the following code into your class it automagically creates get_attributename() and set_attributename() methods dynamically at runtime. So you dont have to manually write 2 methods for every attribute. If you make a typo when accessing an attribute you get a nice error message saying that no method exists.

sub AUTOLOAD { # AUTOLOAD object accessor/mutator method no strict "refs"; # allow me access to the symbol table my ($self,$newval) = @_; return if $AUTOLOAD =~ /::DESTROY$/o; # let perl handle its own cl +ean up if ($AUTOLOAD =~/.*::get(_\w+)/ && $self->_accessible($1, 'read')) +{ my $attr_name = $1; *{$AUTOLOAD} = sub{ return $_[0]->{$attr_name}; }; # Creates a +n encapsulated method in the symbol table return $self->{$attr_name}; } # determine set or get method if ($AUTOLOAD =~/.*::set(_\w+)/ && $self->_accessible($1, 'write') +){ my $attr_name = $1; *{$AUTOLOAD} = sub{ return $_[0]->{$attr_name}=$_[1]; }; return $self->{$attr_name}=$newval; } # no method for this object attribute die "No such method!: $AUTOLOAD"; }

Replies are listed 'Best First'.
Re^2: Should I use Fields, InsideOuts, or Properties?
by stvn (Monsignor) on Jul 06, 2005 at 16:56 UTC
    Encapsulation of an object means you should never access attributes directly but use accessors and mutators(getters and setters).

    This is only partially true. Encapsulation means that you have private data which is not accessible from the "outside" (or more specifically, you can control the accessibility). What you are doing with AUTOLOAD is no better then just accessing the underlying hash directly.

    IMO accessors and mutators should only be created when a truly valid need exists for them. Objects are meant to be a collection of (encapsulated) data and behaviors, but if all your behaviors are simply accessors/mutators, then you might as well just have a struct/record-type.

    -stvn
Re^2: Should I use Fields, InsideOuts, or Properties?
by tphyahoo (Vicar) on Jul 06, 2005 at 14:51 UTC
    This reminds me of perltoot, which also discusses me autoload ways to accomplish accessor/mutator magic; much obliged, I had forgotten to look here.

    FWIW, your example turns off strict, but in perltoot, seems to get the job done with use strict still on. Not sure if this is because of a difference in the examples.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://472808]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found