Created
March 31, 2012 11:31
-
-
Save shelling/2262496 to your computer and use it in GitHub Desktop.
class variables in Moose
This file contains hidden or 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 |
This file contains hidden or 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", | |
| }, | |
| ); | |
| }; |
This file contains hidden or 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; | |
| } | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment