NAME

Gears::Config::Reader::PerlScript - Configuration reader for Perl scripts

SYNOPSIS

use Gears::Config;
use Gears::Config::Reader::PerlScript;

my $reader = Gears::Config::Reader::PerlScript->new(
	declared_vars => {
		env => sub { $ENV{$_[0]} },
	},
);

my $config = Gears::Config->new(readers => [$reader]);
$config->add(file => 'config.pl');

# config.pl can contain:
# {
#     database => {
#         host => env('DB_HOST'),
#         port => 5432,
#     },
#     debug => false,
# }

DESCRIPTION

Gears::Config::Reader::PerlScript reads configuration from Perl script files that return hash references. The scripts are evaluated in a restricted sandbox with strict and warnings enabled, and have access to true and false from builtin.

This reader provides an include function automatically, allowing configuration files to include other files relative to their location. Additional functions or values can be made available through the declared_vars attribute.

Example configuration file:

{
	app => {
		name => 'My Application',
		version => '1.0',
	},
	database => include('db_config.yml'),
	features => {
		cache => true,
		debug => false,
	},
}

INTERFACE

Attributes

declared_vars

A hash reference of variables to make available in the configuration script. Values can be either scalars or code references. Scalars are exposed as functions returning that value, while code references are exposed as functions calling that code with passed arguments.

Available in constructor

Example:

my $reader = Gears::Config::Reader::PerlScript->new(
	declared_vars => {
		hostname => 'localhost',          # a => hostname,
		env => sub { $ENV{$_[0]} },       # b => env(HOME),
	},
);

Methods

new

$object = $class->new(%args)

A standard Mooish constructor. Consult "Attributes" section to learn what keys can key passed in %args.

handled_extensions

@extensions = $reader->handled_extensions()

Returns ('pl'), indicating this reader handles files with the .pl extension.

parse

$hash_ref = $reader->parse($config, $filename)

Evaluates the Perl script in the file and returns the resulting hash reference. The script is evaluated in a clean package namespace with strict and warnings enabled.

Raises Gears::X::Config if there is an error evaluating the script.