in reply to Using the strict module in object oriented programming

Yes, this is one of the standard complaints with the usual method of creating objects in Perl. I can think of two options for you, but both of them are quite a lot of work.

Firstly, you could stop using the hash contents directly and only access your objects' attributes with accessor methods. You can even do this for object access within the class itself.

Secondly you can take a look at Inside-Out Objects. This is a new way of building objects in Perl. It's becoming quite popular and it specifically addresses your problem.

Update: Changed reference to something a bit more recent. But see also the references in xdg's post below.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

  • Comment on Re: Using the strict module in object oriented programming

Replies are listed 'Best First'.
Re^2: Using the strict module in object oriented programming
by Ovid (Cardinal) on Jul 25, 2006 at 13:59 UTC

    And just to throw my hat in the ring in favor of inside-out objects, I liked 'em when I first saw 'em, then I saw issues with serialization, destruction, threading, debugging, and so on. Unfortunately, I've been spoiled with a couple of years of working with really strong programmers. Then I move to a codebase with lots of crufty inherited code and programmers trying to work with this and I've completely changed my mind about inside-out objects. I strongly recommend them.

    Cheers,
    Ovid

    New address of my CGI Course.

      ... then I saw issues with serialization, destruction, threading, ...
      Object::InsideOut takes care of all these issues for you.

      Remember: There's always one more bug.
Re^2: Using the strict module in object oriented programming
by xdg (Monsignor) on Jul 25, 2006 at 13:52 UTC
    Secondly you can take a look at Inside-Out Objects

    Ick. No offense, but that's a really outdated reference on the subject. My scratchpad has a section "Object-Oriented Perl - Inside-Out Technique" that has a more comprehensive set of Perlmonks posts on inside-out objects. For an even more comprehensive treatment, my YAPC::NA inside-out tutorial slides are online.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      My scratchpad has a section "Object-Oriented Perl - Inside-Out Technique"
      Please do not point to your scratchpad in nodes. A scratchpad is not an official place for publication, it can change any time you like and there's nothing we can do about it, actually, it is intended to be changed at will.

      If you really feel your stuff is worth our while, for making it public in a permanent way, then publicize it somewhere more permanent. Like, here. Why point to your scratchpad, if you can just copy the relevant contents?

Re^2: Using the strict module in object oriented programming
by Codon (Friar) on Jul 25, 2006 at 17:18 UTC
    I would highly recommend using Inside Out objects. They keep others from dipping into the internals of your objects and thereby tying your hands through their poor practices.

    Inside Out object do have limitations of sometimes being inconvenient for you as a user, but you as the maintainer can minimize those inconveniences by providing methods to dump the object, etc.

    Perhaps more importantly for large systems, though, is the fact that method accessors can impose a performance hit by requiring the method call (as opposed to the hash look-up). So make sure that your accessors are well optimized in these situations.

    If you have massive amounts of code that treat your objects as hashes and you don't want to refactor all of that code, you could look at refactoring your object to actually have a hash-based interface, but do method calls internallys. Lookups to non-existant "hash keys" could throw an exception. Take a look at the internals Regexp::Common (if you can follow it). It basically provides a nested hash interface, but each level of nesting acts both as a string (returns a pattern) and as a hash ref for deeper nesting. Rather cool. This could let you provide a strict "hash-like" interface to your objects.

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.