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.

#!/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";
which produces the output:
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).

--
I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

In reply to Inside Out Classes and the internal Hashes by KurtSchwind

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.