Created
May 8, 2012 22:12
-
-
Save rsrchboy/2639832 to your computer and use it in GitHub Desktop.
Emulating multiple attributes
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
has config => ( | |
traits => ['Hash'], | |
is => 'bare', | |
isa => 'HashRef[Str]', | |
lazy => 1, | |
# returns a hashref of key/value config pairs | |
builder => 'load_config', | |
handles => { | |
has_author => [ exists => 'author' ], | |
author => [ get => 'author' ], | |
has_email => [ exists => 'email' ], | |
email => [ get => 'email' ], | |
}, | |
); |
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
my $foo = Foo->new(); | |
$foo->has_author; | |
$foo->author; | |
$foo->has_email; | |
$foo->email; |
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
has author => ( | |
is => 'ro', | |
isa => 'Str', | |
lazy => 1, | |
predicate => 'has_author', | |
default => sub { shift->load_config->{author} }, | |
); | |
sub load_config { | |
my ($self) = @_; | |
... some operation to load a config ... | |
return $config_hashref; | |
} |
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
has author => ( | |
is => 'ro', | |
isa => 'Str', | |
lazy => 1, | |
predicate => 'has_author', | |
default => sub { shift->load_config->{author} }, | |
); | |
has email => ( | |
is => 'ro', | |
isa => 'Str', | |
lazy => 1, | |
predicate => 'has_email', | |
default => sub { shift->load_config->{email} }, | |
); | |
sub load_config { | |
my ($self) = @_; | |
... some operation to load a config ... | |
return $config_hashref; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment