Skip to content

Instantly share code, notes, and snippets.

@shelling
Created March 31, 2012 11:31
Show Gist options
  • Select an option

  • Save shelling/2262496 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/2262496 to your computer and use it in GitHub Desktop.
class variables in Moose
#!/usr/bin/env perl
#-*- mode: cperl -*-
use Modern::Perl;
use Person;
my $shelling = Person->new(name => "shelling");
my $count = $shelling->count;
say $count->value; #=> 1
my $sherry = Person->new(name => "sherry");
say $count->value; #=> 2
my $appollo = Person->new(name => "appollo");
say $count->value; #=> 3
use Modern::Perl;
use MooseX::Declare;
class Counter {
has value => (
is => "rw",
isa => "Num",
traits => ['Counter'],
default => 0,
handles => {
inc => "inc",
},
);
};
use Modern::Perl;
use MooseX::Declare;
use Counter;
class Person {
has name => (
is => "ro",
isa => "Str",
default => "nobody",
required => 1,
);
has count => (
is => "rw",
isa => "Counter",
default => sub {
state $count = Counter->new;
$count;
},
);
sub BUILD {
my ($self) = shift;
$self->count->inc;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment