Config-Wild

 view release on metacpan or  search on metacpan

lib/Config/Wild.pm  view on Meta::CPAN

package Config::Wild;

# ABSTRACT: parse an application configuration file with wildcard keywords

use strict;
use warnings;

our $VERSION = '2.02';

use custom::failures __PACKAGE__ . '::Error' => [ 'exists', 'read', 'parse' ];

use Carp;

use List::Util qw[ first ];
use File::pushd;
use Path::Tiny qw[ path cwd ];

use Try::Tiny;

use Log::Any '$log';

use namespace::clean;

sub new {
    my $this = shift;
    my $class = ref( $this ) || $this;

    my %attr = (
        UNDEF      => undef,    # function to call from value when
                                # keyword not defined
        dir        => undef,
        path       => undef,
        ExpandWild => 0,        # match wildcards when expanding
    );

    my $attr = ref $_[-1] eq 'HASH' ? pop @_ : {};

    ## no critic (ProhibitAccessOfPrivateData)
    $attr{$_} = $attr->{$_}
      for
      grep { CORE::exists( $attr{$_} ) or croak( "unknown attribute: $_\n" ) }
      keys %$attr;

    croak( "options dir and path may not both be specified\n" )
      if defined $attr{dir} && defined $attr{path};

    my $self = {
        wild => [],       # regular expression keywords
        abs  => {},       # absolute keywords
        attr => \%attr,
    };

    bless $self, $class;

    my $file = shift;

    croak( "extra arguments passed to new. forgot a hashref?\n" )
      if @_;

    $self->load( $file )
      if $file;

    return $self;
}

sub load {
    my ( $self, $file ) = @_;

    croak( 'no file specified' )
      if !defined $file;

    my $cwd
      = defined $self->{attr}{dir}
      ? pushd( $self->{attr}{dir} )
      : cwd;

    $self->_read_config( $file, path( $cwd ) );



( run in 2.563 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )