Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
In my first real code using Moose, I noticed some things about attributes.

Basically, it is not nice to have to call functions to get and to set each time. I noticed later that some use of "native traits" delegation can mitigate that, but not completely, as it would lead to a proliferation of specific methods; but that's an issue really because Perl built-in types like arrays are not themselves accessed via method syntax and built-in functions don't take references.

The real issue, that can't be put off on other things still needing to catch up, concern very simple types like plain numbers and strings.

Consider:

my $linenum= $self->input_line_number; ++$linenum; $self->input_line_number ($linenum);
Or perhaps$self->input_line_number(1+$self->input_line_number);. But, recall that the ++ syntax is beloved by millions and for a reason! It is quite a bit out of the way to not be able to manipulate things in-place.

Now in Perl6, the accessors return lvalues, so something like ++$self->input_line_number would work just fine.

So here is a specific question: why not have an lvalue accessor in Moose? That could be enabled on request, or the default when it is declared to be a non-reference type.

More generally, how are people coping with the large affordance distance between the method's code and the per-class instance data? It's always been large in Perl 5, but at least I could refer to the slots (via hash access) as lvalues. It seems to be a chore, all the time, for code to get to its values. In C++ you just say x in scope; in Moose you have to say $self->x twice, to get and again to set, and work on a copy.


In reply to Some thoughts on Moose Attributes by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found