| 1 | package MarpaX::Languages::Dash::Config; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | |||
| 7 | use Config::Tiny; | ||
| 8 | |||
| 9 | use File::HomeDir; | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | |||
| 13 | use Path::Tiny; # For path(). | ||
| 14 | |||
| 15 | use Types::Standard qw/Any Str/; | ||
| 16 | |||
| 17 | has config => | ||
| 18 | ( | ||
| 19 | default => sub{return {} }, | ||
| 20 | is => 'rw', | ||
| 21 | isa => Any, | ||
| 22 | required => 0, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has config_file_path => | ||
| 26 | ( | ||
| 27 | default => sub{return ''}, | ||
| 28 | is => 'rw', | ||
| 29 | isa => Any, | ||
| 30 | required => 0, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has section => | ||
| 34 | ( | ||
| 35 | default => sub{return ''}, | ||
| 36 | is => 'rw', | ||
| 37 | isa => Str, | ||
| 38 | required => 0, | ||
| 39 | ); | ||
| 40 | |||
| 41 | our $VERSION = '1.00'; | ||
| 42 | |||
| 43 | # ----------------------------------------------- | ||
| 44 | |||
| 45 | sub BUILD | ||
| 46 | { | ||
| 47 | my($self) = @_; | ||
| 48 | my($path) = path(File::HomeDir -> my_dist_config('MarpaX-Languages-Dash'), '.htmarpax.languages.dash.conf'); | ||
| 49 | |||
| 50 | $self -> read($path); | ||
| 51 | |||
| 52 | } # End of BUILD. | ||
| 53 | |||
| 54 | # ----------------------------------------------- | ||
| 55 | |||
| 56 | sub read | ||
| 57 | { | ||
| 58 | my($self, $path) = @_; | ||
| 59 | |||
| 60 | $self -> config_file_path($path); | ||
| 61 | |||
| 62 | # Check [global]. | ||
| 63 | |||
| 64 | $self -> config(Config::Tiny -> read($path) ); | ||
| 65 | |||
| 66 | if (Config::Tiny -> errstr) | ||
| 67 | { | ||
| 68 | die Config::Tiny -> errstr; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self -> section('global'); | ||
| 72 | |||
| 73 | if (! ${$self -> config}{$self -> section}) | ||
| 74 | { | ||
| 75 | die "Config file '$path' does not contain the section [@{[$self -> section]}]\n"; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Check [x] where x is host=x within [global]. | ||
| 79 | |||
| 80 | $self -> section(${$self -> config}{$self -> section}{'host'}); | ||
| 81 | |||
| 82 | if (! ${$self -> config}{$self -> section}) | ||
| 83 | { | ||
| 84 | die "Config file '$path' does not contain the section [@{[$self -> section]}]\n"; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Move desired section into config, so caller can just use $self -> config to get a hashref. | ||
| 88 | |||
| 89 | $self -> config(${$self -> config}{$self -> section}); | ||
| 90 | |||
| 91 | } # End of read. | ||
| 92 | |||
| 93 | # -------------------------------------------------- | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | =pod | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | MarpaX::Languages::Dash::Config - A config manager for use by MarpaX::Languages::Dash | ||
| 102 | |||
| 103 | =head1 Synopsis | ||
| 104 | |||
| 105 | End-users do not need to call the methods in this module. | ||
| 106 | |||
| 107 | See L<MarpaX::Languages::Dash>. | ||
| 108 | |||
| 109 | =head1 Description | ||
| 110 | |||
| 111 | L<MarpaX::Languages::Dash::Config> provides a config manager for use by L<MarpaX::Languages::Dash>. | ||
| 112 | |||
| 113 | =head1 Installation | ||
| 114 | |||
| 115 | Install L<MarpaX::Languages::Dash> as you would for any C<Perl> module: | ||
| 116 | |||
| 117 | Run: | ||
| 118 | |||
| 119 | cpanm MarpaX::Languages::Dash | ||
| 120 | |||
| 121 | or run: | ||
| 122 | |||
| 123 | sudo cpan MarpaX::Languages::Dash | ||
| 124 | |||
| 125 | or unpack the distro, and then either: | ||
| 126 | |||
| 127 | perl Build.PL | ||
| 128 | ./Build | ||
| 129 | ./Build test | ||
| 130 | sudo ./Build install | ||
| 131 | |||
| 132 | or: | ||
| 133 | |||
| 134 | perl Makefile.PL | ||
| 135 | make (or dmake or nmake) | ||
| 136 | make test | ||
| 137 | make install | ||
| 138 | |||
| 139 | =head1 Methods | ||
| 140 | |||
| 141 | =head2 read() | ||
| 142 | |||
| 143 | read() is called automatically by new(). It does the actual reading of the config file. | ||
| 144 | |||
| 145 | If the file can't be read, C<die $string> is called. | ||
| 146 | |||
| 147 | The path to the config file is determined by: | ||
| 148 | |||
| 149 | path(File::HomeDir -> my_dist_config('MarpaX-Languages-Dash'), '.htmarpax.languages.dash.conf'); | ||
| 150 | |||
| 151 | The author runs scripts/copy.config.pl, which uses similar code, to move the config file | ||
| 152 | from the config/ directory in the disto into an OS-dependent directory. | ||
| 153 | |||
| 154 | The run-time code uses this module to look in the same directory as used by scripts/copy.config.pl. | ||
| 155 | |||
| 156 | =head1 Support | ||
| 157 | |||
| 158 | Email the author, or log a bug on RT: | ||
| 159 | |||
| 160 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX::Languages::Dash>. | ||
| 161 | |||
| 162 | =head1 Author | ||
| 163 | |||
| 164 | L<MarpaX::Languages::Dash> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2013. | ||
| 165 | |||
| 166 | Home page: L<http://savage.net.au/index.html>. | ||
| 167 | |||
| 168 | =head1 Copyright | ||
| 169 | |||
| 170 | Australian copyright (c) 2013, Ron Savage. | ||
| 171 | |||
| 172 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 173 | you can redistribute them and/or modify them under the terms of | ||
| 174 | The Artistic License, a copy of which is available at: | ||
| 175 | http://www.opensource.org/licenses/index.html | ||
| 176 | |||
| 177 | =cut |