I'm starting to learn Moose, and object-oriented programming in Perl in general. My apologies if I get the terms wrong or I'm missing something obvious.

Is there a way to create an attribute in Moose that is read-only to the outside world, but that the code in the package can change?

My application: the class stores various attributes (a representation of a Perforce changelist, as it happens). There is an immutable integer key (changelist number). It is storing these instances in an array indexed by the key. I would like the class to allow the caller to get the minimum and maximum key values seen so far. (It is not destroying any instances while the program is running, so these can be adjusted via sub BUILD.) But a caller should not be able to set them.

Since I'm learning Moose and Perl OO, I'd like to do it in "the standard Moose way", if any. (For example, the program outside the class could keep track of the min and max, but I'd like the class to track them instead.) Also, being naive, I'm thinking that Moose probably handles lots of grungy details correctly, and I wouldn't.

My thought was MooseX::ClassAttribute. But with class_has 'min_change' => (is => 'ro', ...), not even the package itself can change it.

I looked at writer=>, but isn't that visible outside too?

Is it kosher to just declare "my" variables (rw internally) and provide non-instance-based subs so that callers can read them (ro outside) -- does that work with inheritance and all that in the Moose framework? Any other notions? I'm afraid I'm at sea here.

Tim McDaniel, tmcd@panix.com


In reply to Moose attribute: ro to public, rw in the class? by Tim McDaniel

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.