Activator
view release on metacpan or search on metacpan
lib/Activator/Options.pm view on Meta::CPAN
=head2 Variable Substitution
#### TODO: NOT YET IMPLEMENTED
Substitution occurs as the last step of processing. Every value for
every key (including values within lists ) are visited. Values for any
key can optionally contain a reference to another key by using C<${}>
notation. Use the indirect operator C<'-E<gt>'> to reference deeply nested
values. For example:
default:
key1: value1
key2: value2
realm1:
foo: bar
realm2:
key2: ${key1}
realm3:
key3: ${realm1->foo}/${key2} # value == 'bar/value2'
key4: ${realm1->foo}/${realm2->key2} # value == 'bar/value1'
Note that you must fully qualify any deeply nested references.
=head1 METHODS
=head2 new()
Constructor: implements singleton. Not very useful. Use L<get_opts()>.
=cut
sub new {
my ( $pkg ) = @_;
my $self = bless( {
REGISTRY => Activator::Registry->new(),
ARGV_EXTRA => {},
ARGV => undef,
BAREWORDS => undef,
}, $pkg);
$self->_init_StrongSingleton();
return $self;
}
=head2 get_opts()
Usage:
Activator::Options->get_opts( \@ARGV ); # default realm
Activator::Options->get_opts( \@ARGV, $realm );
Strip recognized options from C<@ARGV> and return the configuration
hash C<$opts> for C<$realm> based on C<@ARGV>. C<$realm> is optional
(default is 'default'), and if not specified either the command line
argument (C<--realm>) or environment variable
(C<ACT_OPT_E<lt>realmE<gt>> unless C<ACT_OPT_skip_env> is set) will be
used. Not specifying a realm via one of these mechanisms is a fatal
error.
Examples:
#### get options for default realm
my $opts = Activator::Options->get_opts( \@ARGV );
#### get options for 'some' realm
my $opts = Activator::Options->get_opts( \@ARGV, 'some' );
See L<get_args()> for a description of the way command line arguments
are processed.
=cut
sub get_opts {
my ( $pkg, $argv, $realm ) = @_;
my $self = &new( @_ );
my $argx = {};
# get_args sets $self->{ARGV}
$self->get_args( $argv );
DEBUG( Data::Dumper->Dump( [ $self->{ARGV} ], [ qw/ ARGV / ] ) );
DEBUG( Data::Dumper->Dump( [ $self->{BAREWORDS} ], [ qw /BAREWORDS/ ] ) );
# make sure we can use ENV vars
my $skip_env = $ENV{ACT_OPT_skip_env};
$realm ||=
$self->{ARGV}->{realm} ||
( $skip_env ? undef : $ENV{ACT_OPT_realm} ) ||
'default';
# setup or get the merged YAML configuration settings from files
# into the registry
my $opts = $self->{REGISTRY}->get_realm( $realm );
# first call
if ( !keys %$opts ) {
# define valid opts from config files
try eval {
$self->_process_config_for( $realm );
};
# _set_reg throws err if $realm is invalid
if ( catch my $e ) {
$e->rethrow;
}
# read environment variables, set any keys found
if ( !$skip_env ) {
my ( $env_key, $env_realm );
foreach my $env_key ( keys %ENV ) {
next unless $env_key =~ /^ACT_OPT_(.+)/;
$opt_key = $1;
$opt_realm = $realm;
my $env_opt_realm = $opt_realm;
my $env_opt_key = $opt_key;
if ( $opt_key =~ /^_(\w+)__(\w+)$/ ) {
$env_opt_realm = $1;
( run in 0.907 second using v1.01-cache-2.11-cpan-39bf76dae61 )