You may want to try using something like a Blessed subroutine as your object.
If you team this with your own AUTOLOAD subrotine, you can really get alot of flexibility
Not only does this method offer you a way to privately encapsulate
your objects internal data structure, but gives you the flexibility of doing some really
cool things with the way the data is stored or retrieved.

The subroutine can have whatever logic you want coded into it.
You can have multiple encapsulated private data objects (array, hashes, scalars etc),
and even build in permissions or validation logic to certain actions or attributes.
The AUTOLOAD routine can be coded to handle most of the simple accessors, saving
you the trouble of coding them all, and your script from compiling them all each run.

This also helps force people to access your object through the accessor methods rather than
getting at the data itself, like what happens with blessed hashes or arrays.

package Bill; use strict; use warnings; our ( $AUTOLOAD ); sub AUTOLOAD { my ( $self, $newval ) = @_; no strict q{refs}; if( $AUTOLOAD =~ /.*::get_(.+)/ ) { my $attr = $1; # Class Attribute name. *{$AUTOLOAD} = sub { $_[0]->( q{get}, $attr ) }; #cache return $self->( q{get}, $attr ); } elsif ( $AUTOLOAD =~ /.*::set_(.+)/ ) { my $attr = $1; *{$AUTOLOAD} = sub { $_[0]->( q{set}, $attr, $_[1] ) }; #cache return $self->( q{set}, $attr, $newval ); } } sub new { my ( $class, $args ) = @_; my %attribs; # Hold and ecapsulate all object attribs. my @protected_attribs = qw( userid access_level ); # This means that rather than being able to get at object # attribs like this... # $self->{obj_attrib} # you have to do this... # $self->( 'get', 'obj_attrib' ); # $self->( 'set', 'obj_attrib', 'new_value' ); # OR through use of the AUTOLOAD routine... # $self->get_obj_attrib; # $self->set_obj_attrib( 'new_value' ); my $self = sub { my ( $cmd, $attr, $newval ) = @_; if ( $cmd eq q{set} ) { unless ( scalar grep { $attr eq $_ } @protected_attribs ) { $attribs{$attr} = $newval } } return $attribs{$attr}; }; $self = bless $self, $class; #----------------------------------------- # Do any initialization that is needed for the objects data $attribs{dbh} = $args->{dbh}; return wantarray ? ( 1, $self ) : $self; } # END new

Damian Conway's Object Oriented Perl book, has more on this type of object structure.
One of the best Perl books I have ever read.

Updated: Added link for Damian's book.

Hope that helps,
Wonko


In reply to Re: On Perl Objects! by Wonko the sane
in thread On Perl Objects! by newbio

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.