Acrux
view release on metacpan or search on metacpan
lib/Acrux/Config.pm view on Meta::CPAN
package Acrux::Config;
use strict;
use utf8;
=encoding utf8
=head1 NAME
Acrux::Config - Config::General Configuration of Acrux
=head1 SYNOPSIS
use Acrux::Config;
my $config = Acrux::Config->new(
file => '/etc/myapp.conf',
);
say $config->get('foo');
=head1 DESCRIPTION
The module works with the configuration using L<Config::General>
All getters of this class are allows get access to configuration parameters by path-pointers.
See L<Acrux::Pointer> and L<RFC 6901|https://tools.ietf.org/html/rfc6901>
=head2 new
my $config = Acrux::Config->new(
file => '/etc/myapp.conf',
default => {foo => 'bar'},
);
=head1 ATTRIBUTES
This plugin supports the following attributes
=head2 default
default => {foo => 'bar'}
Default configuration data
=head2 dirs
dirs => ['/etc/foo', '/etc/bar']
Paths to additional directories of config files
=head2 file
file => '/etc/foo.stuff'
Path to configuration file, absolute or relative, defaults to the value of the
C<$0.conf> in the current directory
=head2 noload
noload => 1
This attribute disables loading config file
=head2 options
options => {'-AutoTrue' => 0}
lib/Acrux/Config.pm view on Meta::CPAN
Returns an hash of found values from configuration
=head2 latest
say $config->latest('/foo'); # ['first', 'second', 'third']
# third
Returns an latest value of found values from configuration
=head2 load
my $config = $config->load;
Loading config files
=head2 pointer
my $pointer = $config->pointer;
Returns current L<Acrux::Pointer> object
=head1 HISTORY
See C<Changes> file
=head1 TO DO
See C<TODO> file
=head1 SEE ALSO
L<Config::General>, L<Acrux::Pointer>
=head1 AUTHOR
Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright (C) 1998-2026 D&D Corporation
=head1 LICENSE
This program is distributed under the terms of the Artistic License Version 2.0
See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
=cut
use Config::General qw//;
use Cwd qw/getcwd/;
use File::Spec qw//;
use File::Basename qw/basename/;
use Acrux::Pointer;
use Acrux::RefUtil qw/as_array is_array_ref is_hash_ref is_value/;
use Acrux::Util qw/clone/;
use constant DEFAULT_CG_OPTS => {
'-ApacheCompatible' => 1, # Makes possible to tweak all options in a way that Apache configs can be parsed
'-LowerCaseNames' => 1, # All options found in the config will be converted to lowercase
'-UTF8' => 1, # All files will be opened in utf8 mode
'-AutoTrue' => 1, # All options in your config file, whose values are set to true or false values, will be normalised to 1 or 0 respectively
};
sub new {
my $class = shift;
my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
my $self = bless {
default => $args->{defaults} || $args->{default} || {},
file => $args->{file} // '',
root => $args->{root} // '', # base path to default files/directories
dirs => $args->{dirs} || [],
noload => $args->{noload} || 0,
options => {},
error => '',
config => {},
pointer => Acrux::Pointer->new,
files => [],
orig => $args->{options} || $args->{opts} || {},
}, $class;
my $myroot = length($self->{root}) ? $self->{root} : getcwd();
# Set dirs
my @dirs = ();
foreach my $dir (as_array($self->{dirs})) {
unless (File::Spec->file_name_is_absolute($dir)) { # rel
$dir = length($myroot)
? File::Spec->rel2abs($dir, $myroot)
: File::Spec->rel2abs($dir);
}
push @dirs, $dir if -e $dir;
}
$self->{dirs} = [@dirs];
# Set config file
my $file = $self->{file};
$file = sprintf("%s.conf", basename($0)) unless length $file;
unless (File::Spec->file_name_is_absolute($file)) { # rel
$file = length($myroot)
? File::Spec->rel2abs($file, $myroot)
: File::Spec->rel2abs($file);
}
$self->{file} = $file;
unless ($self->{noload}) {
unless (-r $file) {
$self->{error} = sprintf("Configuration file \"%s\" not found or unreadable", $file);
return $self;
}
}
# Config::General Options
my $orig = $self->{orig};
$orig = {} unless is_hash_ref($orig);
my %options = (%{DEFAULT_CG_OPTS()}, %$orig); # Merge everything
$options{'-ConfigFile'} = $file;
$options{"-ConfigPath"} ||= [@dirs] if scalar(@dirs);
$self->{options} = {%options};
# Load
return $self if $self->{noload};
return $self->load;
( run in 0.604 second using v1.01-cache-2.11-cpan-39bf76dae61 )