Skip to content

Instantly share code, notes, and snippets.

@rsrchboy
Created May 8, 2012 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsrchboy/2639832 to your computer and use it in GitHub Desktop.
Save rsrchboy/2639832 to your computer and use it in GitHub Desktop.
Emulating multiple attributes
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' ],
},
);
my $foo = Foo->new();
$foo->has_author;
$foo->author;
$foo->has_email;
$foo->email;
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;
}
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