The state variable in CODE reference may be competent with this role.
A Counter as an example here. We define a Counter class here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Modern::Perl; | |
use MooseX::Declare; | |
class Counter { | |
has value => ( | |
is => "rw", | |
isa => "Num", | |
traits => ['Counter'], | |
default => 0, | |
handles => { | |
inc => "inc", | |
}, | |
); | |
}; |
A counter has a attribute "value" and a method "inc" to increment it.
Then We define a Person class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
The Person class has a attribute "name" and a shared attribute "count". The Counter is a state variable return from a anonymous CODE reference. That's the trick.
The BUILD hook increment the counter everytime a new Person is constructed.
Then we examinate the shared variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
A global variable in package name may still work. But this way uses 'has' syntax sugar. :p
If you use builder to replace the default option. the subroutine used by the builder is just the class variable accessor from Class name, returning value as $person->count().