In the thread 'Set' Method Return Value, I recommended that members that can be set and got should be accessed directly (or tied to hide the accessors), rather than having a method-based API to set and get them. This idea was downvoted (from what I surmise) because it exposes the implementation.

But it doesn't.

With tie, a hash is just an API. And it's a good API for any object that has settable-gettables, because it's familiar and can be used in all the ways that Perl users know and love (subject to any restrictions you program in, of course). It also encapsulates your object completely: all access is necessarily through the API.

That's it. What follows is some example code of minimal objects implemented in the getter-setter way, and then using a hash API. You can see that the code is very similar. Both implementations use an array as the actual base type.

Update: I should add that the code below is not intended to be a blueprint for writing classes. It is only to illustrate the notion that a hash can be an API rather than an implementation. There are a number of issues to address (pointed out by respondants already) to make a workable, general-purpose foundation.

use warnings; use strict; package MyThing; sub new { my $class = shift; bless [], $class; } sub set_foo { my ($self, $newfoo) = @_; $self->[0] = $newfoo; } sub get_foo { $_[0]->[0]; } package HashAPI; sub TIEHASH { my $class = shift; bless [], $class; } sub STORE { my ($self, $key, $val) = @_; unless ($key eq 'foo') { die "No $key member\n"; } $self->[0] = $val; } sub FETCH { my ($self, $key, $val) = @_; unless ($key eq 'foo') { die "No $key member\n"; } $self->[0]; } sub new { my $class = shift; tie my(%obj), $class; bless \%obj, $class; } package main; my $clunker = MyThing->new(); my $slick = HashAPI->new(); $clunker->set_foo('Some value'); print $clunker->get_foo, "\n"; $slick->{foo} = 'Some value'; print $slick->{foo}, "\n";

Caution: Contents may have been coded under pressure.

In reply to tie for Perlish, encapsulated objects 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.