Activator
view release on metacpan or search on metacpan
lib/Activator/Options.pm view on Meta::CPAN
default:
name: David Davidson from Deluth, Delaware
some_realm:
name: Sally Samuelson from Showls, South Carolina
other_realm:
name: Ollie Oliver from Olive Branch, Oklahoma
overrides:
default:
name: Ron Johnson from Ronson, Wisconson
some_realm:
name: Johnny Jammer, the Rhode Island Hammer
Would produce the following C<$opts>:
$opts = {
default => {
name => 'Ron Johnson from Ronson, Wisconson',
},
some_realm => {
name => 'Johnny Jammer, the Rhode Island Hammer',
},
other_realm => {
name => 'Ron Johnson from Ronson, Wisconson',
},
}
=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 );
( run in 2.990 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )