in reply to Re^2: OOP By Any Other Name
in thread OOP By Any Other Name
Ah, I wasn't sure just what aspect of the code you were most interested in. A more traditional approach to inside out objects goes something like this:
use strict; use warnings; use Data::Dump; package Counter; my %data; sub new { my ($class, $start) = @_; my $lu; my $self = bless \$lu, $class; $data{$lu = "$self"} = {count => $start}; return $self; } sub add { my ($self, $value) = @_; $self = $data{$$self}; $self->{count} += $value; } sub get { my ($self) = @_; $self = $data{$$self}; return $self->{count}; } package main; my $ctr = Counter->new (7); print "Start: " . $ctr->get () . "\n"; $ctr->add (3); print "End: " . $ctr->get () . "\n";
which gains the data hiding without losing Perl's normal OO support.
The largest downside I see with the code you suggested is declaring all the anonymous subs in the constructor - that just doesn't scale well to anything other than toy code and is syntactically much nastier in my view.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: OOP By Any Other Name
by tilly (Archbishop) on Feb 26, 2011 at 03:02 UTC |