Skip to content

Instantly share code, notes, and snippets.

@Getty
Created July 10, 2019 20:38
Show Gist options
  • Save Getty/b95aa5bdd61d39ab6baaf9cefd6efc8a to your computer and use it in GitHub Desktop.
Save Getty/b95aa5bdd61d39ab6baaf9cefd6efc8a to your computer and use it in GitHub Desktop.
sub has_conf {
my ( $name, $env_key, $default ) = @_;
my $default_ref = ref $default;
has $name => (
is => 'ro',
lazy => 1,
default => sub {
my ( $self ) = @_;
my $result;
if ($self->always_use_default) {
if ($default_ref eq 'CODE') {
$result = $default->(@_);
} else {
$result = $default;
}
} else {
if (defined $ENV{$env_key}) {
$result = $ENV{$env_key};
} else {
if ($default_ref eq 'CODE') {
$result = $default->(@_);
} else {
$result = $default;
}
}
}
return $result;
},
);
}
####################################
# ____ ___ _ _ _____ ___ ____
# / ___/ _ \| \ | | ___|_ _/ ___|
# | | | | | | \| | |_ | | | _
# | |__| |_| | |\ | _| | | |_| |
# \____\___/|_| \_|_| |___\____|
#
has_conf is_live => MYAPP_LIVE => 0;
has_conf no_cache => MYAPP_NO_CACHE => 0;
has_conf cachedir => MYAPP_CACHEDIR => sub { tempdir( CLEANUP => 1 ) };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment