Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: Making 'all' the attributes read only by default (Moo)

by blindluke (Hermit)
on Jan 23, 2015 at 11:07 UTC ( [id://1114265]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Making 'all' the attributes read only by default (Moo)
in thread Making 'all' the attributes read only by default (Moo)

Can the Boo constant consume a role? I don't think so.

I know that your replies are usually both knowledgeable and helpful, as I've learned much from many of them, so I'll skip over the 'angst' remark and try to provide an example that better conveys my aim:

package Ghost { use Moo; with('Attacker'); has 'Str' => ( is => 'ro', default => sub { '8' } ); has 'Dex' => ( is => 'ro', default => sub { '16' } ); has 'Con' => ( is => 'ro', default => sub { '10' } ); has 'Int' => ( is => 'ro', default => sub { '10' } ); has 'Wis' => ( is => 'ro', default => sub { '10' } ); has 'Cha' => ( is => 'ro', default => sub { '6' } ); } my $boo = Ghost->new( Int => '14', Con => '16' );

I'm asking about a way to state that all the attributes of Ghost are readonly, as I think that:

package Ghost { use Moo; use Moo::AllAttributesAreReadonly; with('Attacker'); has 'Str' => ( default => sub { '8' } ); has 'Dex' => ( default => sub { '16' } ); has 'Con' => ( default => sub { '10' } ); has 'Int' => ( default => sub { '10' } ); has 'Wis' => ( default => sub { '10' } ); has 'Cha' => ( default => sub { '6' } ); } my $boo = Ghost->new( Int => '14', Con => '16' );

or something like this, would be much more elegant than the way I am using now. If my "classes" did not consume roles, had no methods, and were as simple as the one I provided, then I would have no reason to use Moo in the first place.

- Luke

Replies are listed 'Best First'.
Re^5: Making 'all' the attributes read only by default (Moo)
by BrowserUk (Patriarch) on Jan 23, 2015 at 13:16 UTC
    I'm asking about a way to state that all the attributes of Ghost are readonly,

    I understand what you are asking for, and can even sympathize with your desire to do away with boilerplate code. Sorry that I cannot help you with that; perhaps tobyink will happen by with some wisdom.

    But for the life of me I do not understand the desire to construct write-once variables at runtime.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found