I need a class attribute in Moose. Right now I am saying:
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use MooseX::Declare;
class User {
has id => (isa => "Str", is => 'ro', builder => '_get_id');
has name => (isa => "Str", is => 'ro');
has balance => (isa => "Num", is => 'rw', default => 0);
#FIXME: this should use a database
method _get_id {
state $id = 0; #I would like this to be a class attribute
return $id++;
}
}
my @users;
for my $name (qw/alice bob charlie/) {
push @users, User->new(name => $name);
};
for my $user (@users) {
print $user->name, " has an id of ", $user->id, "\n";
}