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

Save yourself some typing and some angst:

use constant Boo => { attr_A => 'A', attr_B => 'B', attr_C => 123, };

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

Replies are listed 'Best First'.
Re^4: Making 'all' the attributes read only by default (Moo)
by blindluke (Hermit) on Jan 23, 2015 at 11:07 UTC

    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

      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