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

Re^3: Moose object construction

by stvn (Monsignor)
on Feb 10, 2011 at 16:10 UTC ( #887490=note: print w/replies, xml ) Need Help??

in reply to Re^2: Moose object construction
in thread Moose object construction

What you want to do is change your visitor attribute to be something like:

has 'visitors' => ( is => 'ro', writer => '_set_visitors', init_arg => undef, );
Then inside your BUILD method, you will want to do:
$self->_set_visitors( ... );
The init_arg being set to "undef" will ensure that no one can do:
MyClass->new( visitors => ... )
But all this said, you should really spend some time reading the Moose::Manual. Several of the mistakes you have made seem to be caused by the fact you are guessing and/or making assumptions of your own about how Moose works, rather then actually taking the time to learn how it actually works. You also might find some of the presentations on helpful as well, there are several excellent introductory ones from recent conferences.

You really too should give a look to MooseX::SimpleConfig as it solves this problem for you and the code has been battle tested already.


Replies are listed 'Best First'.
Re^4: Moose object construction
by thargas (Deacon) on Feb 10, 2011 at 17:17 UTC
    Thanks for this. I wasn't aware that you could have a writer defined for a ro attribute. It still seems wrong to me that a writer should exist, and work, on a ro attribute, but it does solve the problem.
      It still seems wrong to me that a writer should exist, and work, on a ro attribute

      Well, really the 'ro' and 'rw' are just shorthand for the various combinations of 'reader', 'writer' and 'accessor'. So a 'ro' attribute is just shorthand for:

      has 'foo' => ( reader => 'foo' );
      And a 'rw' attribute is just shorthand for:
      has 'foo' => ( accessor => 'foo' );
      It is also quite possible to do really weird things like:
      has 'foo' => ( reader => 'get_foo', writer => 'set_foo', accessor => ' +foo' );
      which may seem odd, but could be very useful in some situations where the API requires it. This is all part of Moose's policy to provide the best practices at your fingertips, but not get in the way when you need to do something weird, which if you think about it is not all that different from Perl itself.

        Thank you all for suggestions!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://887490]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-06-19 00:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.