The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

App::PerlShell::Config - Perl Shell Config

SYNOPSIS

use App::PerlShell::Config;
my $config = App::PerlShell::Config->new(
    key => 'value'
);
print $config->config;
print $config->config('key');
$config->config('key', 'new_value');
print $config->config('key');

DESCRIPTION

App::PerlShell::Config creates a global configuration structure for App::PerlShell applications.

METHODS

new() - create a new Config object

my $config = App::PerlShell::Config->new(
    key1 => 'value1',
    key2 => 'value2',
    ...
);

Create a new App::PerlShell::Config object with provided key / value pairs as configuration options.

config() - get / set configuration parameters

[$c =] $config->config([OPTIONS]);

Get or set configuration parameters configured with new. This allows a user of App::PerlShell to manipulate configuration parameters but not add new ones with this method interface.

In a App::PerlShell program / module, one may choose to subclass this method with:

sub config {
    $config->config(@_)
}

This allows for manipulation of the configuration parameters without knowing the object variable ($config in the above example).

Get all:

[$i =] $config->config();
[%i =] $config->config();

Called with no options, returns all configuration parameters as reference or hash, depending on how it's called. In App::PerlShell, called with no return value simply prints all configuration parameters.

Get one:

[$i =] $config->config('key');

Returns the value of key.

Set:

[$i =] $config->config('key','new_value');
[$i =] $config->config(key => 'new_value');

Sets the value of key to 'new_value' and returns the previous value.

add() - add configuration parameters

$config->add(key [,value]);

Add 'key' with optional 'value' to the App::PerlShell::Config object. If 'value' not provided, 'key' is added with value undef. Returns 1 on success, 0 on failure.

delete() - delete configuration parameters

[$c =] $config->delete(key);

Delete 'key' from the App::PerlShell::Config object. Returns 1 on success, 0 on failure.

exists() - check for existence of configuration parameters

[$c =] $config->exists(key);

Check if 'key' exists in the App::PerlShell::Config object. Returns 1 if yes, 0 if not.

SUBROUTINES

config_where - find config file provided

$conf_file = config_where($conf [,$dir]);

Given a config file name ($conf) and an optional directory ($dir), find the config file and return the full path or undefined if not found.

Search order is:

  1. '$conf' in the current working directory where the script is invoked. If found, return value is simply the file name in '$conf', no path.

  2. '$conf' in user's home directory (e.g., $HOME, %USERPROFILE%).

  3. '$conf' in the provided directory '$dir'. This can be any directory, but usually would be the installation directory of the script as such:

    use FindBin qw($Bin);
    ...
    $conf_file = config_where($conf, $Bin);

If no configuration file is found, returns undefined.

EXPORTS

Subroutine config_where can be exported by calling use with:

use App::PerlShell::Config qw(config_where);

SEE ALSO

App::PerlShell

LICENSE

This software is released under the same terms as Perl itself. If you don't know what that means visit http://perl.com/.

AUTHOR

Copyright (c) 2015 Michael Vincent

http://www.VinsWorld.com

All rights reserved