Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

At the moment, I generally just bless a hash and access its members (e.g. $self->{member}). As Abigail-II pointed out, though, this can make misspellings into tricky runtime errors. Also, the syntax seems kind of ugly to me.

Perhaps I'm being a bit dull here, but you could just use what is often called 'good manners', but is sometimes referred to as true OO. That is knock at the front door rather than sneak in through the cellar and use proper getter and setter methods to access your attributes and you can even do this without using things like Class::MethodMaker.

Of course, writing a load of getters and setters can get boring. Using AUTOLOAD without safeguards might cause all sorts of autovivification which is dangerous (I should know, I do it all the time).

Therefore the best way, IMHO, is the way that TheDamian suggests in Chapter 3 of his book, Object Oriented Perl, pp 91ff. This is to set up a hash to look after accessibility, like this:

{ my %_attr = { _attr1 => 'read', _attr2 => 'write', } sub _accessible { my ($self, $attr, $mode) = @_; $_attr{$attr} =~ /$mode/; } }

Then lower down you can have your AUTOLOAD:

sub AUTOLOAD { my ($self, $value) = @_; ## was it a get method? $AUTOLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1, 'read') and re +turn $self->{$1}; ## was it a set method? $AUOTLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1, 'write') and d +o { $self->{$1} = $value; return} die "No such method: $AUTOLOAD, $!"; }

Now, your program will die if you try to access an attribute that doesn't exist. Perhaps it would be better if it threw an exception if you try to set an attribute that is read-only, but in that version it doesn't.


In reply to Re: How do YOU do OO in Perl? by Nomad
in thread How do YOU do OO in Perl? by batkins

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2017-10-22 21:17 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (275 votes). Check out past polls.