Aion-Env

 view release on metacpan or  search on metacpan

lib/Aion/Env/Etc.pm  view on Meta::CPAN

	
	test:
	  abc: -12

File etc/test.yml:

	test:
	  abc: 100
	
	when@dev:
	  test:
	    val: 10



	BEGIN { $ENV{APP_ENV} = 'dev' }
	
	sub Int { sub { /^-?\d+$/ } }
	
	use Aion::Env::Etc TEST_ABC => (isa => Int);
	use Aion::Env::Etc VAL => (isa => Int, key => 'test.val');
	
	TEST_ABC # -> -12
	VAL # -> 10

=head1 DESCRIPTION

Parses the configuration file. The path to it is specified by the environment variable C<AION_ENV_ETC_PATH>.

It may contain the C<includes> key with the inclusion of other configuration files, and those with other ones.
For simplicity, C<includes> are triggered from the current directory, which should correspond to the project root (this is the convention).

Keys of the form C<when@ID> will overlap with their keys the keys of the configuration file if the C<ID> of them matches C<APP_ENV>.

Hashes in keys, if the keys match in different files, are combined recursively. However, if one of the keys does not have a hash, an exception will be thrown.

=head1 SUBROUTINES

=head2 import ($name, %kw)

Creates a constant in the package from which it was called.

Valid options:

=over

=item * C<isa> – tester routine or C<Aion::Type> object for type checking.

=item * C<default> – default value.

=item * C<key> – key from configuration files. By default, the name of the constant is converted to it (translated to lower case and underscores are replaced with dots).

=back

=head2 parse ($path)

Reads and parses a configuration file in C<yaml> format. C<${ID}> are replaced with values from C<%ENV>, and if not there, then from the C<.env> file. Parses files in C<include> recursively.

=head2 merge_hashes ($file, $path, $x, $y)

Concatenates two hashes recursively. If the matching keys do not have hashes, then it throws an error with C<$file> and C<$path>, where C<$file> is the connecting file, and C<$path> is the path from the keys through the dot.

=head2 val ($s)

Adds backslashes. Used for escaping environments.

	my $escape_string = "\\\"\\'\\\\\\t\\r\\n";
	Aion::Env::Etc::val("\"'\\\t\r\n") # -> $escape_string

=head2 by_key ($hash, $path)

Get the value by key from the hash.

	my ($val, $key_exists) = Aion::Env::Etc::by_key({x => {y => {z => 3}}}, "x.y.z");
	
	$val # -> 3
	$key_exists # -> 1
	
	($val, $key_exists) = Aion::Env::Etc::by_key({x => {y => {t => 10}}}, "x.y.z");
	
	$val # -> undef
	$key_exists # -> 0

=head1 AUTHOR

Yaroslav O. Kosmina L<mailto:dart@cpan.org>

=head1 LICENSE

âš– B<Perl5>

=head1 COPYRIGHT

The Aion::Env::Etc module is copyright © 2026 Yaroslav O. Kosmina. Rusland. All rights reserved.



( run in 0.554 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )