NAME
Venus::Config - Config Class
ABSTRACT
Config Class for Perl 5
SYNOPSIS
package main;
use Venus::Config;
my $config = Venus::Config->new;
# $config = $config->read_file('app.pl');
# "..."
DESCRIPTION
This package provides methods for loading Perl, YAML, and JSON configuration files, and fetching configuration information.
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
edit_file
edit_file(string $file, string | coderef $code) (Venus::Config)
The edit_file method does an in-place edit, i.e. it loads a Perl, YAML, or JSON configuration file, passes the decoded data to the method or callback provided, and writes the results of the method or callback to the file.
Since 3.10
- edit_file example 1
-
package main; use Venus::Config; my $config = Venus::Config->edit_file('t/conf/edit.perl', sub { my ($self, $data) = @_; $data->{edited} = 1; return $data; }); # bless(..., 'Venus::Config')
new
new(any @args) (Venus::Config)
The new method constructs an instance of the package.
Since 4.15
- new example 1
-
package main; use Venus::Config; my $config = Venus::Config->new; # bless(..., "Venus::Config")
- new example 2
-
package main; use Venus::Config; my $config = Venus::Config->new(value => {password => 'secret'}); # bless(..., "Venus::Config")
read_env
read_env(string $data) (Venus::Config)
The read_env method returns a new Venus::Config object based on the string of key/value pairs provided. This method supports multiline values when enclosed in double or single quotes.
Since 4.15
- read_env example 1
-
# given: synopsis package main; my $read_env = $config->read_env( "APPNAME=Example\nAPPVER=0.01\n# Comment\n\n\nAPPTAG=\"Godzilla\"", ); # bless(..., 'Venus::Config')
- read_env example 2
-
# given: synopsis package main; my $read_env = $config->read_env( "MESSAGE=\"Hello\nWorld\"\nSIGNATURE='Best,\nTeam'", ); # bless(..., 'Venus::Config')
- read_env example 3
-
# given: synopsis package main; my $read_env = $config->read_env( 'ESCAPE="line1\nline2\ttabbed"', ); # bless(..., 'Venus::Config')
read_env_file
read_env_file(string $file) (Venus::Config)
The read_env_file method uses Venus::Path to return a new Venus::Config object based on the file provided.
Since 4.15
- read_env_file example 1
-
# given: synopsis package main; $config = $config->read_env_file('t/conf/read.env'); # bless(..., 'Venus::Config')
read_file
read_file(string $path) (Venus::Config)
The read_file method load a Perl, YAML, or JSON configuration file, based on the file extension, and returns a new Venus::Config object.
Since 2.91
- read_file example 1
-
package main; use Venus::Config; my $config = Venus::Config->read_file('t/conf/read.perl'); # bless(..., 'Venus::Config')
- read_file example 2
-
package main; use Venus::Config; my $config = Venus::Config->read_file('t/conf/read.json'); # bless(..., 'Venus::Config')
- read_file example 3
-
package main; use Venus::Config; my $config = Venus::Config->read_file('t/conf/read.yaml'); # bless(..., 'Venus::Config')
read_json
read_json(string $data) (Venus::Config)
The read_json method returns a new Venus::Config object based on the JSON string provided.
Since 2.91
- read_json example 1
-
# given: synopsis package main; $config = $config->read_json(q( { "$metadata": { "tmplog": "/tmp/log" }, "$services": { "log": { "package": "Venus/Path", "argument": { "$metadata": "tmplog" } } } } )); # bless(..., 'Venus::Config')
read_json_file
read_json_file(string $file) (Venus::Config)
The read_json_file method uses Venus::Path to return a new Venus::Config object based on the file provided.
Since 2.91
- read_json_file example 1
-
# given: synopsis package main; $config = $config->read_json_file('t/conf/read.json'); # bless(..., 'Venus::Config')
read_perl
read_perl(string $data) (Venus::Config)
The read_perl method returns a new Venus::Config object based on the Perl string provided.
Since 2.91
- read_perl example 1
-
# given: synopsis package main; $config = $config->read_perl(q( { '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } } )); # bless(..., 'Venus::Config')
read_perl_file
read_perl_file(string $file) (Venus::Config)
The read_perl_file method uses Venus::Path to return a new Venus::Config object based on the file provided.
Since 2.91
- read_perl_file example 1
-
# given: synopsis package main; $config = $config->read_perl_file('t/conf/read.perl'); # bless(..., 'Venus::Config')
read_yaml
read_yaml(string $data) (Venus::Config)
The read_yaml method returns a new Venus::Config object based on the YAML string provided.
Since 2.91
- read_yaml example 1
-
# given: synopsis package main; $config = $config->read_yaml(q( '$metadata': tmplog: /tmp/log '$services': log: package: "Venus/Path" argument: '$metadata': tmplog )); # bless(..., 'Venus::Config')
read_yaml_file
read_yaml_file(string $file) (Venus::Config)
The read_yaml_file method uses Venus::Path to return a new Venus::Config object based on the YAML string provided.
Since 2.91
- read_yaml_file example 1
-
# given: synopsis package main; $config = $config->read_yaml_file('t/conf/read.yaml'); # bless(..., 'Venus::Config')
write_env
write_env() (string)
The write_env method returns a string representing environment variable key/value pairs based on the "value" held by the underlying Venus::Config object. Multiline values are escaped using \n notation and enclosed in double quotes.
Since 4.15
- write_env example 1
-
# given: synopsis package main; my $value = $config->value({ APPNAME => "Example", APPTAG => "Godzilla", APPVER => 0.01, }); my $write_env = $config->write_env; # "APPNAME=Example\nAPPTAG=Godzilla\nAPPVER=0.01"
- write_env example 2
-
# given: synopsis package main; my $value = $config->value({ MESSAGE => "Hello\nWorld", NOTE => "line1\ttabbed", }); my $write_env = $config->write_env; # "MESSAGE=\"Hello\\nWorld\"\nNOTE=\"line1\\ttabbed\""
- write_env example 3
-
# given: synopsis package main; my $value = $config->value({ APPNAME => "Example", MESSAGE => "Hello\nWorld\nGoodbye", APPTAG => "Godzilla", }); my $write_env = $config->write_env; my $read_env = $config->read_env($write_env); # bless(..., 'Venus::Config') # round-trip: read_env(write_env($value)) == $value
write_env_file
write_env_file(string $path) (Venus::Config)
The write_env_file method saves a environment configuration file and returns a new Venus::Config object.
Since 4.15
- write_env_file example 1
-
# given: synopsis my $value = $config->value({ APPNAME => "Example", APPTAG => "Godzilla", APPVER => 0.01, }); $config = $config->write_env_file('t/conf/write.env'); # bless(..., 'Venus::Config')
write_file
write_file(string $path) (Venus::Config)
The write_file method saves a Perl, YAML, or JSON configuration file, based on the file extension, and returns a new Venus::Config object.
Since 2.91
- write_file example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus/Path", argument => { value => "." } } } }); $config = $config->write_file('t/conf/write.perl'); # bless(..., 'Venus::Config')
- write_file example 2
-
# given: synopsis my $value = $config->value({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); $config = $config->write_file('t/conf/write.json'); # bless(..., 'Venus::Config')
- write_file example 3
-
# given: synopsis my $value = $config->value({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); $config = $config->write_file('t/conf/write.yaml'); # bless(..., 'Venus::Config')
write_json
write_json() (string)
The write_json method returns a JSON encoded string based on the "value" held by the underlying Venus::Config object.
Since 2.91
- write_json example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus::Path" }, }, }); my $json = $config->write_json; # '{ "$services":{ "log":{ "package":"Venus::Path" } } }'
write_json_file
write_json_file(string $path) (Venus::Config)
The write_json_file method saves a JSON configuration file and returns a new Venus::Config object.
Since 2.91
- write_json_file example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus/Path", argument => { value => "." } } } }); $config = $config->write_json_file('t/conf/write.json'); # bless(..., 'Venus::Config')
write_perl
write_perl() (string)
The write_perl method returns a FILE encoded string based on the "value" held by the underlying Venus::Config object.
Since 2.91
- write_perl example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus::Path" }, }, }); my $perl = $config->write_perl; # '{ "\$services" => { log => { package => "Venus::Path" } } }'
write_perl_file
write_perl_file(string $path) (Venus::Config)
The write_perl_file method saves a Perl configuration file and returns a new Venus::Config object.
Since 2.91
- write_perl_file example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus/Path", argument => { value => "." } } } }); $config = $config->write_perl_file('t/conf/write.perl'); # bless(..., 'Venus::Config')
write_yaml
write_yaml() (string)
The write_yaml method returns a FILE encoded string based on the "value" held by the underlying Venus::Config object.
Since 2.91
- write_yaml example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus::Path" }, }, }); my $yaml = $config->write_yaml; # '---\n$services:\n\s\slog:\n\s\s\s\spackage:\sVenus::Path'
write_yaml_file
write_yaml_file(string $path) (Venus::Config)
The write_yaml_file method saves a YAML configuration file and returns a new Venus::Config object.
Since 2.91
- write_yaml_file example 1
-
# given: synopsis my $value = $config->value({ '$services' => { log => { package => "Venus/Path", argument => { value => "." } } } }); $config = $config->write_yaml_file('t/conf/write.yaml'); # bless(..., 'Venus::Config')
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
Copyright (C) 2022, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.