KurtSchwind has asked for the wisdom of the Perl Monks concerning the following question:
I've been working on some OO perl and I'm just starting to work with Inside out classes. Every example I see is very similar and I can emulate it without a problem.
However, I've taken a different approach and I can't find an example that does it the way I plan on doing it. So am I just way off here? Is there a major flaw in my method?
Here seems to be the traditional inside-out class :
package PointX; use strict; use warnings; use Scalar::Util qw(refaddr); use Carp qw(carp cluck croak confess); our $VERSION = '0.1b'; { my %xcord; my %ycord; sub new { my ($class, $cords) = @_; my $new_object = bless \do{my $anon_scalar;}, $class; $xcord{refaddr $new_object} = $cords->{'x'}; $ycord{refaddr $new_object} = $cords->{'y'}; return $new_object; } sub DESTROY { my ($self) = @_; delete $cord{refaddr $self}; return; } sub set_x { my($self, $xcord) = @_; $xcord{refaddr $self} = $xcord; return ($xcord{refaddr $self}); } sub get_x { my($self) = @_; return ($xcord{refaddr $self}); } sub set_y { my($self, $ycord) = @_; $ycord{refaddr $self} = $ycord; return ($ycord{refaddr $self}); } sub get_y { my($self) = @_; return ($ycord{refaddr $self}); } } # end of internal scoping. 1;
But am I wrong if I get rid of the multiple hashes and go with a single hash ref like this?
package PointX; use strict; use warnings; use Scalar::Util qw(refaddr); use Carp qw(carp cluck croak confess); our $VERSION = '0.1b'; { my %cord; sub new { my ($class, $cords) = @_; my $new_object = bless \do{my $anon_scalar;}, $class; $cord{refaddr $new_object}->{'x'} = $cords->{'x'}; $cord{refaddr $new_object}->{'y'} = $cords->{'y'}; return $new_object; } sub DESTROY { my ($self) = @_; delete $cord{refaddr $self}; return; } sub set_x { my($self, $xcord) = @_; $cord{refaddr $self}->{'x'} = $xcord; return ($cord{refaddr $self}->{'x'}); } sub get_x { my($self) = @_; return ($cord{refaddr $self}->{'x'}); } sub set_y { my($self, $ycord) = @_; $cord{refaddr $self}->{'y'} = $ycord; return ($cord{refaddr $self}->{'y'}); } sub get_y { my($self) = @_; return ($cord{refaddr $self}->{'y'}); } } # end of internal scoping. 1;
In the second example, I wrote a test program that seems to work just fine and as I'd expect.
which produces the output:#!/usr/bin/perl use strict; use warnings; use PointX; my $point = PointX->new({x=>3, y=>20}); print "The x cord is ".$point->get_x()."\n"; print "The y cord is ".$point->get_y()."\n"; $point->set_x(10); print "The x cord is ".$point->get_x()."\n"; print "The y cord is ".$point->get_y()."\n";
The x cord is 3 The y cord is 20 The x cord is 10 The y cord is 20
So why do most things just keep adding internal hashes instead of using 1 hash for everything? I'm new to this inside out class way of doing things, so I want to nip any bad habits in the bud (if indeed this is a bad habit).
This is a trivial example of just having 2 items tracked with a single hash. My current application will track around a dozen. I'd prefer not to have a dozen hashes, but I will if it's deemed to be The-Right-Thing (tm).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Inside Out Classes and the internal Hashes
by kyle (Abbot) on Dec 17, 2007 at 21:05 UTC | |
Re: Inside Out Classes and the internal Hashes
by Joost (Canon) on Dec 17, 2007 at 21:14 UTC | |
by bart (Canon) on Dec 18, 2007 at 13:53 UTC | |
Re: Inside Out Classes and the internal Hashes
by shmem (Chancellor) on Dec 17, 2007 at 22:02 UTC | |
Re: Inside Out Classes and the internal Hashes
by FunkyMonk (Chancellor) on Dec 17, 2007 at 22:32 UTC | |
by KurtSchwind (Chaplain) on Dec 18, 2007 at 13:49 UTC | |
by kyle (Abbot) on Dec 18, 2007 at 17:01 UTC | |
by TStanley (Canon) on Dec 18, 2007 at 17:22 UTC | |
by flexo (Sexton) on Dec 18, 2007 at 15:52 UTC |