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');
# my $name = $config->resolve('name');
# "..."
DESCRIPTION
This package provides methods for loading Perl, YAML, and JSON configuration files, fetching configuration information, and building objects with dependency injection.
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
metadata
metadata(Str $name) (Any)
The metadata method returns the $metadata
section of the configuration data if no name is provided, otherwise returning the specific metadata keyed on the name provided.
Since 1.95
- metadata example 2
-
# given: synopsis package main; $config = $config->read_perl(q( { '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } } )); my $metadata = $config->metadata; # { # tmplog => "/tmp/log" # }
- metadata example 3
-
# given: synopsis package main; $config = $config->read_perl(q( { '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } } )); my $metadata = $config->metadata("tmplog"); # "/tmp/log"
read_file
read_file(Str $path) (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(Str $data) (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') # my $log = $config->resolve('log');
read_json_file
read_json_file(Str $file) (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') # my $log = $config->resolve('log');
read_perl
read_perl(Str $data) (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') # my $log = $config->resolve('log');
read_perl_file
read_perl_file(Str $file) (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') # my $log = $config->resolve('log');
read_yaml
read_yaml(Str $data) (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') # my $log = $config->resolve('log');
read_yaml_file
read_yaml_file(Str $file) (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') # my $log = $config->resolve('log');
reify
reify(Str $name) (Any)
The reify method resolves and returns an object or value based on the service name provided.
Since 1.95
- reify example 1
-
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); my $reify = $config->reify('tmp'); # undef
- reify example 2
-
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); my $reify = $config->reify('log'); # bless({value => '/tmp/log'}, 'Venus::Path')
- reify example 3
-
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); my $reify = $config->reify('log', '.'); # bless({value => '.'}, 'Venus::Path')
- reify example 4
-
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } }); my $reify = $config->reify('log', {value => '.'}); # bless({value => '.'}, 'Venus::Path')
resolve
resolve(Str $name) (Any)
The resolve method resolves and returns an object or value based on the configuration key or service name provided.
Since 1.95
- resolve example 1
-
package main; use Venus::Config; my $config = Venus::Config->new({ name => 'app', log => '/tmp/log/app.log', '$metadata' => { tmplog => "/tmp/log", varlog => "/var/log" }, '$services' => { log => { package => "Venus/Path", argument => '.' }, tmp_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "tmplog" } }, var_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "varlog" } } } }); my $result = $config->resolve; # undef
- resolve example 2
-
package main; use Venus::Config; my $config = Venus::Config->new({ name => 'app', log => '/tmp/log/app.log', '$metadata' => { tmplog => "/tmp/log", varlog => "/var/log" }, '$services' => { log => { package => "Venus/Path", argument => '.' }, tmp_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "tmplog" } }, var_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "varlog" } } } }); my $result = $config->resolve('log'); # "/tmp/log/app.log"
- resolve example 3
-
package main; use Venus::Config; my $config = Venus::Config->new({ name => 'app', log => '/tmp/log/app.log', '$metadata' => { tmplog => "/tmp/log", varlog => "/var/log" }, '$services' => { log => { package => "Venus/Path", argument => '.' }, tmp_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "tmplog" } }, var_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "varlog" } } } }); my $result = $config->resolve('tmp_log'); # bless({value => '/tmp/log'}, 'Venus::Path')
- resolve example 4
-
package main; use Venus::Config; my $config = Venus::Config->new({ name => 'app', log => '/tmp/log/app.log', '$metadata' => { tmplog => "/tmp/log", varlog => "/var/log" }, '$services' => { log => { package => "Venus/Path", argument => '.' }, tmp_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "tmplog" } }, var_log => { package => "Venus/Path", extends => 'log', argument => { '$metadata' => "varlog" } } } }); my $result = $config->resolve('var_log'); # bless({value => '/var/log'}, 'Venus::Path')
services
services(Str $name) (Any)
The services method returns the $services
section of the configuration data if no name is provided, otherwise returning the specific service keyed on the name provided.
Since 1.95
- services example 2
-
# given: synopsis package main; $config = $config->read_perl(q( { '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } } )); my $services = $config->services; # { # log => { # package => "Venus/Path", # argument => {'$metadata' => "tmplog"} # } # }
- services example 3
-
# given: synopsis package main; $config = $config->read_perl(q( { '$metadata' => { tmplog => "/tmp/log" }, '$services' => { log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } } } } )); my $services = $config->services('log'); # { # package => "Venus/Path", # argument => {'$metadata' => "tmplog"} # }
write_file
write_file(Str $path) (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() (Str)
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(Str $path) (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() (Str)
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(Str $path) (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() (Str)
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(Str $path) (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')
FEATURES
This package provides the following features:
- $callback
-
This package supports resolving services as callbacks to be passed around and/or resolved by other services. The
$callback
directive is used to specify the name of a service to be resolved and passed as an argument.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { log => { package => "Venus/Path", argument => '.', }, lazy_log => { package => "Venus/Code", argument => { '$callback' => 'log', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('lazy_log'); # bless(..., 'Venus::Code') # my $return = $result->call; # bless(..., 'Venus::Path')
- $envvar
-
This package supports inlining environment variables as arguments to services. The
$envvar
directive is used to specify the name of an environment variable, and can also be used in metadata for reusability.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { home => { package => "Venus/Path", argument => { '$envvar' => 'home', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('home'); # bless(..., 'Venus::Path')
example 2
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { home => { '$envvar' => 'home', } }, '$services' => { home => { package => "Venus/Path", argument => { '$metadata' => 'home', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('home'); # bless(..., 'Venus::Path')
- $function
-
This package supports inlining the result of a service resolution and function call as arguments to services. The
#
delimited$function
directive is used to specify the name of an existing service on the right-hand side, and an arbitrary function to be call on the result on the left-hand side.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { filespec => { package => 'File/Spec/Functions', }, tempdir => { package => "Venus/Path", argument => { '$function' => 'filespec#tmpdir', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('tempdir'); # bless(..., 'Venus::Path')
- $metadata
-
This package supports inlining configuration data as arguments to services. The
$metadata
directive is used to specify the name of a stashed configuration value or data structure.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { home => '/home/ubuntu', }, '$services' => { home => { package => "Venus/Path", argument => { '$metadata' => 'home', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('home'); # bless(..., 'Venus::Path')
- $method
-
This package supports inlining the result of a service resolution and method call as arguments to services. The
#
delimited$method
directive is used to specify the name of an existing service on the right-hand side, and an arbitrary method to be call on the result on the left-hand side.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { filespec => { package => 'File/Spec', }, tempdir => { package => "Venus/Path", argument => { '$method' => 'filespec#tmpdir', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('tempdir'); # bless(..., 'Venus::Path')
- $routine
-
This package supports inlining the result of a service resolution and routine call as arguments to services. The
#
delimited$routine
directive is used to specify the name of an existing service on the right-hand side, and an arbitrary routine to be call on the result on the left-hand side.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { filespec => { package => 'File/Spec', }, tempdir => { package => "Venus/Path", argument => { '$routine' => 'filespec#tmpdir', } } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('tempdir'); # bless(..., 'Venus::Path')
- $service
-
This package supports inlining resolved services as arguments to other services. The
$service
directive is used to specify the name of a service to be resolved and passed as an argument.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { 'path' => { 'package' => 'Venus/Path', }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('path'); # bless(..., 'Venus::Path')
- #argument
-
This package supports providing static and/or dynamic arguments during object construction from metadata or other services.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { 'date' => { 'package' => 'Venus/Date', 'argument' => 570672000, }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
example 2
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { 'date' => { 'package' => 'Venus/Date', 'argument' => { year => 1988, month => 2, day => 1, }, }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
- #argument_as
-
This package supports transforming the way static and/or dynamic arguments are passed to the operation during object construction. Acceptable options are
array
orarrayref
(which provides an arrayref),hash
orhashref
(which provides a hashref), orlist
(which provides a flattened list of arguments).example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { 'date' => { 'package' => 'Venus/Date', 'argument' => { year => 1988, month => 2, day => 1, }, argument_as => 'list', }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
example 2
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { 'date' => { 'package' => 'Venus/Date', 'argument' => { year => 1988, month => 2, day => 1, }, argument_as => 'hash', }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
- #builder
-
This package supports specifying multiple build steps as
function
,method
, androutine
calls and chaining them together. Each build step supports any directive that can be used outside of a build step. Each build step can be configured, with thereturn
directive, to use a particular value to chain the next subroutine call. Acceptablereturn
values areclass
(package name string),result
(scalar return value from the current build step), andself
(instantiated package). Additionally, you can use theinject
directive (with any value accepted byargument_as
) to override the default arguments using the arguments provided to the "reify" or "resolve" method.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { datetime => { package => "Venus/Date", builder => [ { method => 'new', argument => 570672000, return => 'self', }, { method => 'string', return => 'result', } ], } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('datetime'); # "1988-02-01T00:00:00Z"
example 2
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { datetime => { package => "Venus/Date", builder => [ { method => 'new', argument => 570672000, return => 'self', inject => 'list', }, { method => 'string', return => 'result', } ], } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('datetime', 604945074); # "1989-03-03T16:17:54Z"
- #config
-
This package supports configuring services and metadata in the service of building objects and values.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ 'name' => 'app', 'secret' => '...', '$metadata' => { home => { '$envvar' => 'home', } }, '$services' => { date => { package => "Venus/Date", }, path => { package => "Venus/Path", argument => { '$metadata' => 'home', }, } } }); # bless(..., 'Venus::Config') # my $path = $config->resolve('path'); # bless(..., 'Venus::Path') # my $name = $config->resolve('name'); # "app"
- #constructor
-
This package supports specifying constructors other than the traditional
new
routine. A constructor is always called with the package name as the invocant.example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { path => { package => "Venus/Path", constructor => "new", } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('path'); # bless(..., 'Venus::Path')
- #extends
-
This package supports extending services in the definition of other services, recursively compiling service configurations and eventually executing the requested compiled service.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { log => { package => "Venus/Log", argument => { level => "trace", }, }, development_log => { package => "Venus/Log", extends => "log", builder => [ { method => "new", return => "self", inject => "hash", } ], }, production_log => { package => "Venus/Log", extends => "log", argument => { level => "error", }, builder => [ { method => "new", return => "self", inject => "hash", } ], }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('development_log'); # bless(..., 'Venus::Log') # my $level = $result->level; # "trace" # $result = $config->resolve('production_log'); # bless(..., 'Venus::Log') # $level = $result->level; # "error"
- #function
-
This package supports specifying construction as a function call, which when called does not provide an invocant.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { foo_hex => { package => "Digest/MD5", function => "md5_hex", argument => "foo", } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('foo_hex'); # "acbd18db4cc2f85cedef654fccc4a4d8"
- #lifecycle
-
This package supports different lifecycle options which determine when services are built and whether they're persisted. Acceptable lifecycle values are
singleton
(which caches the result once encountered) andeager
(which caches the service upon the first execution of any service).example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { match => { package => "Venus/Match", argument => { 'a'..'h' }, builder => [ { method => "new", return => "result", }, { method => "data", return => "result", inject => "hash", } ], lifecycle => 'eager', } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('thing'); # undef # my $result = $config->resolve('match'); # bless(..., 'Venus::Match')
example 2
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { match => { package => "Venus/Match", argument => { 'a'..'h' }, builder => [ { method => "new", return => "result", }, { method => "data", return => "result", inject => "hash", } ], lifecycle => 'singleton', } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('match'); # bless(..., 'Venus::Match')
- #metadata
-
This package supports specifying data and structures which can be used in the construction of multiple services.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$metadata' => { 'homedir' => '/home', 'tempdir' => '/tmp', }, '$services' => { home => { package => "Venus/Path", argument => { '$metadata' => 'homedir', }, }, temp => { package => "Venus/Path", argument => { '$metadata' => 'tempdir', }, }, } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('home'); # bless(..., 'Venus::Path') # my $result = $config->resolve('temp'); # bless(..., 'Venus::Path')
- #method
-
This package supports specifying construction as a method call, which when called provides the package or object instance as the invocant.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { date => { package => "Venus/Date", argument => 570672000, method => "new", } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
- #routine
-
This package supports specifying construction as a function call, which when called provides the package as the invocant.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { date => { package => "Venus/Date", argument => 570672000, routine => "new", } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('date'); # bless(..., 'Venus::Date')
- #service
-
This package supports defining services to be constructed on-demand or automatically on instantiation.
example 1
package main; use Venus::Config; my $config = Venus::Config->new({ '$services' => { path => { package => "Venus/Path", } } }); # bless(..., 'Venus::Config') # my $result = $config->resolve('path'); # bless(..., 'Venus::Path')
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
Copyright (C) 2000, Al Newkirk.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.