Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: On Perl Objects!

by Wonko the sane (Deacon)
on Oct 24, 2004 at 13:23 UTC ( [id://402030]=note: print w/replies, xml ) Need Help??


in reply to On Perl Objects!

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found