CTKlib

 view release on metacpan or  search on metacpan

lib/CTK/Configuration.pm  view on Meta::CPAN

    my $value = $config->conf( 'key' );

Gets value from config structure by key

    my $config_hash = $config->conf;

Returns config hash structure

=item B<get>

    my $value = $config->get( 'key' );

Gets value from config structure by key

=item B<getall>

    my $config_hash = $config->getall;

Returns config hash structure

=item B<load>

    my $config = $config->load;

Loading config files

=item B<reload>

    my $config = $config->reload;

Reloading config files. All the previous config options will be flushes

=item B<set>

    $config->set( 'key', 'value' );

Sets value to config structure by key. Returns setted value

=item B<status>

    print $config->error unless $config->status;

Returns boolean status of loading config file

=back

=head1 HISTORY

=over 8

=item B<1.00 Mon Apr 29 10:36:06 MSK 2019>

Init version

=back

See C<Changes> file

=head1 DEPENDENCIES

L<Config::General>, L<Try::Tiny>

=head1 TO DO

See C<TODO> file

=head1 BUGS

* none noted

=head1 SEE ALSO

L<Config::General>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See C<LICENSE> file and L<https://dev.perl.org/licenses/>

=cut

use vars qw($VERSION);
$VERSION = '1.01';

use Carp;
use Config::General;
use Try::Tiny;
use Time::HiRes qw/gettimeofday/;
use Cwd qw/getcwd/;
use File::Spec ();

use constant {
    CONF_DIR    => "conf",
    LOCKED_KEYS => [qw/hitime loadstatus/],
};

sub new {
    my $class = shift;
    my %args = @_;

    # Create object
    my $myhitime = gettimeofday() * 1;
    my $self = bless {
        status  => 0,
        dirs    => [],
        error   => "",
        files   => [],
        created => time(),
        orig    => {},
        myhitime=> $myhitime,
        conf    => {
            hitime      => $myhitime,
            loadstatus  => 0, # == $self->{status}
        },
    }, $class;

    # Set dirs
    my @dirs = ();
    my $mydir = $args{confdir} // $args{dir};
    my $root = getcwd();
    my $confdir;
    if ($mydir) {
        $confdir = File::Spec->file_name_is_absolute($mydir)
            ? $mydir
            : File::Spec->catdir($root, $mydir);
        push (@dirs, $root) unless File::Spec->file_name_is_absolute($mydir);
    } else {
        $confdir = File::Spec->catdir($root, CONF_DIR);
        push (@dirs, $root);
    }
    push(@dirs, $confdir) if length($confdir);
    push(@dirs, CONF_DIR) if $confdir ne CONF_DIR;
    $self->{dirs} = [@dirs];

    # Set files
    my $fileconf = $args{config} // $args{file} // $args{fileconf};
    unless ($fileconf) {
        $self->{error} = "Config file not specified";
        return $self;
    }
    $fileconf = File::Spec->catfile($root, $fileconf)
        unless File::Spec->file_name_is_absolute($fileconf);
    $self->{files} = [$fileconf];
    unless (-e $fileconf) {
        $self->{error} = sprintf("Config file not found: %s", $fileconf);
        return $self;
    }



( run in 0.558 second using v1.01-cache-2.11-cpan-39bf76dae61 )