Here is a contrived example:

#!/usr/bin/env perl use 5.010_000; use strict; use warnings; { package Polygon; use Moose; my $SIDES = 10; sub sides { my ($self, $new_sides) = @_; # Class or Instance ... if ( ! blessed $self ) { $self = $self->new; # This is a bit of a hack ... obviousl +y =) } # Set sides if ($new_sides) { $self->{_SIDES} = \$new_sides; # You can make $SIDES reado +nly here. } # Get sides return ${ $self->{_SIDES} }; } sub BUILD { my ( $self, $params ) = @_; $self->sides( $params->{_SIDES} // $SIDES ); return $self; } no Moose; 1; } { package Triangle; use Moose; use Carp qw(carp); extends qw(Polygon); sub BUILD { my ( $self, $params ) = @_; if ( $params->{_SIDES} ) { carp sprintf 'Cannot alter SIDES with value (%s)', $params +->{_SIDES}; } $self->{_SIDES} = \3; # ALWAYS 3 SIDES return $self; } no Moose; 1; } say {*STDERR} Polygon->sides; # 10 say {*STDERR} Polygon->new->sides; # 10 say {*STDERR} Polygon->new( _SIDES => 20 )->sides; # 20 say {*STDERR} Triangle->sides; # 3 say {*STDERR} Triangle->new->sides; # 3 say {*STDERR} Triangle->new( _SIDES => 4 )->sides; # 3 with warning m +essage

UPDATE: Just seen stvn's post ... do that instead =)

UPDATE: To do this with stvn's recommendation, this example would look like so:

#!/usr/bin/env perl use 5.010_000; use strict; use warnings; { package Polygon; use Moose; use MooseX::ClassAttribute; class_has 'sides' => ( is => 'rw', isa => 'Int', default => sub { 10 }, ); __PACKAGE__->meta->make_immutable(); #MooseX::ClassAttribute::containing_class()->meta()->make_immutabl +e(); no Moose; no MooseX::ClassAttribute; 1; } { package Triangle; use Moose; use MooseX::ClassAttribute; extends 'Polygon'; class_has '+sides' => ( default => sub { 3 }, ); __PACKAGE__->meta->make_immutable(); #MooseX::ClassAttribute::containing_class()->meta()->make_immutabl +e(); no Moose; no MooseX::ClassAttribute; 1; } say {*STDERR} Polygon->sides; # 10 say {*STDERR} Polygon->new->sides; # 10 say {*STDERR} Triangle->sides; # 3 say {*STDERR} Triangle->new->sides; # 3 exit 0;

Don't know why the commented line #MooseX::ClassAttribute::containing_class()->meta()->make_immutable(); does not quite work. Its an open ticket on the RT queue =)


Smoothie, smoothie, hundre prosent naturlig!

In reply to Re: Class variables in Moose? by j1n3l0
in thread Class variables in Moose? by Anonymous Monk

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.