Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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