Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Should I use Fields, InsideOuts, or Properties?

by BrowserUk (Pope)
on Jul 06, 2005 at 11:13 UTC ( #472758=note: print w/ replies, xml ) Need Help??


in reply to Should I use Fields, InsideOuts, or Properties?

A couple of options:

  1. Use blessed arrays and constants for your objects. Smaller, faster and safer.
  2. Initialise your blessed hashes with the required fields and set them readonly manually.
    use Internals qw(SetReadOnly);; $fred = bless {},'fred';; @$fred{ qw[ jack john bill ] } = ();; SetReadOnly $fred;; print Dumper $fred;; $VAR1 = bless( { 'john' => undef, 'jack' => undef, 'bill' => undef }, 'fred' ); $fred->{bill} = 3;; print Dumper $fred;; $VAR1 = bless( { 'john' => undef, 'jack' => undef, 'bill' => 3 }, 'fred' ); $fred->{derf} = 'error';; Attempt to access disallowed key 'derf' in a restricted hash at (eval +11) line 1, <STDIN> line 9. print Dumper $fred;; $VAR1 = bless( { 'john' => undef, 'jack' => undef, 'bill' => 3 }, 'fred' );

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.


Comment on Re: Should I use Fields, InsideOuts, or Properties?
Download Code
Re^2: Should I use Fields, InsideOuts, or Properties?
by tphyahoo (Vicar) on Jul 06, 2005 at 11:27 UTC
    Thanks for mentioning this.

    Reading the thread you linked to, adrianh commented "Once you start refactoring classes and moving object fields between classes, adding fields to classes, etc. I find array based objects becomes a complete PITA." and you seem to agree with him. So, I am reading your example as a way to do things very cleanly in some simple cases, but not as a replacement for fields, which, despite its annoyances, does play well with inheritance.

      It's true that for both methods you need to code your constructors with a view to inheritance, but you have to do that when using fields also.

      For the manually lock hash, you just have to test whether you are constructing a new instance or adding to an existing class, and in the latter case, unlock the hash, add you fields and re-lock. I admit that doesn't provide the _\w* is private behaviour.

      For the blessed arrays, inheritance is messy. Hence my agreement with adrianh.

      Basically, I have still to find the perfect combination for OO in Perl, but I don't have cause to make a great deal of use of it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re^2: Should I use Fields, InsideOuts, or Properties?
by nothingmuch (Priest) on Jul 06, 2005 at 15:31 UTC
    BTW, for those who don't know, use fields in 5.9 and up is basically the same as the second solution - it creates a hash with locked keys.
    -nuffin
    zz zZ Z Z #!perl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-09-30 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (367 votes), past polls