The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Buildable

Venus::Role::Valuable

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')

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_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.