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::CELL::Load -- find and load message files and config files

VERSION

Version 0.088

SYNOPSIS

    use App::CELL::Load;

    # Load App::CELL's internal messages and config params and then
    # attempt to load the application's messages and config params
    $status = App::CELL::Load::init();
    return $status if $status->not_ok;

    # attempt to determine the site configuration directory
    my $siteconfdir = App::CELL::Load::get_siteconfdir();

    # get a reference to a list of configuration files (full paths) of a
    # given type under a given directory
    my $metafiles = App::CELL::Load::find_files( '/etc/CELL', 'meta' );
   
    # load messages from all message file in a given directory and all its
    # subdirectories
    $status = message_files( '/etc/CELL' );

    # load meta, core, and site params from all meta, core, and site
    # configuration files in a given directory and all its subdirectories
    $status = meta_core_site_files( '/etc/CELL' );

DESCRIPTION

The purpose of the App::CELL::Load module is to provide message and config file finding and loading functionality to the App::CELL::Message and App::CELL::Config modules.

init

Re-entrant initialization function.

On first call, initializes all three site configuration hashes by performing the following actions:

1. load App::CELL's internal messages and meta, core, and site params from the distro share directory determined using File::ShareDir
2. look for a site configuration directory by consulting (a) the environment, (b) ~/.cell/CELL.conf, (c) /etc/sysconfig/perl-CELL, (d) /etc/CELL. Error exit on fail.
3. load messages and meta, core, and site params from the site configuration directory.

Subsequent calls check state variables to determine status of previous calls. For example, it might happen that the first call only loads the App::CELL configuration, but not the site configuration, etc.

Sets the following App::CELL site params:

CELL_META_DISTRO_SHAREDIR_LOADED - meta param
CELL_DISTRO_SHAREDIR_FULLPATH - site param
CELL_META_SITECONF_DIR_LOADED - meta param
CELL_META_SITECONF_DIR_FULLPATH - site param

Takes: nothing; returns: status object. To be called like this:

    my $status = App::CELL::Load::init();

Return status is ok provided at least the distro sharedir was found and loaded.

message_files

Loads message files from the given directory. Takes: full path to configuration directory. Returns: result hash containing 'quantfiles' (total number of files processed) and 'count' (total number of messages loaded).

meta_core_site_files

Loads meta, core, and site config files from the given directory. Takes: full path to configuration directory. Returns: result hash containing 'quantfiles' (total number of files processed) and 'count' (total number of configuration parameters loaded).

get_siteconfdir

Look in various places (in a pre-defined order) for the site configuration directory. Stop as soon as we come up with a viable candidate. On success, returns a string containing an absolute directory path. On failure, returns undef.

_read_siteconfdir_from_file

Takes the full path of what might be a configuration file containing SITECONF_PATH setting. Returns that setting (which is a possible site conf directory) on success, undef on failure.

find_files

Takes two arguments: full directory path and config file type.

Always returns an array reference. On "failure", the array reference will be empty.

How it works: first, the function checks a state variable to see if the "work" of walking the configuration directory has already been done. If so, then the function simply returns the corresponding array reference from its cache (the state hash %resultlist). If this is the first invocation for this directory, the function walks the directory (and all its subdirectories) to find files matching one of the four regular expressions corresponding to the four types of configuration files('meta', 'core', 'site', 'message'). For each matching file, the full path is pushed onto the corresponding array in the cache.

Note that only CELL_MAX_CONFIG_FILES will be loaded.

parse_message_file

This function is where message files are parsed. It takes a PARAMHASH consisting of:

File - filename (full path)
Dest - hash reference (where to store the message templates).

Returns: number of stanzas successfully parsed and loaded

parse_config_file

Parses a configuration file and adds the parameters found to the hashref provided. If a parameter already exists in the hashref, a warning is generated, the existing parameter is not overwritten, and processing continues.

This function doesn't care what type of configuration parameters are in the file, except that they must be scalar values. Since the configuration files are actually Perl modules, the value can even be a reference (to an array, a hash, or a subroutine, or any other complex data structure).

The technique used in the eval, derived from Request Tracker, can be described as follows: a local typeglob "set" is defined, containing a reference to an anonymous subroutine. Subsequently, a config file (Perl module) consisting of calls to this "set" subroutine is required.

Note: If even one call to set fails to compile, the entire file will be rejected and no configuration parameters from that file will be loaded.

The parse_config_file function takes a PARAMHASH consisting of:

File - filename (full path)
Dest - hash reference (where to store the config params).

Returns: number of configuration parameters parsed/loaded

(IMPORTANT NOTE: If even one call to set fails to compile, the entire file will be rejected and no configuration parameters from that file will be loaded.)

_conf_from_config

This function takes a target hashref (which points to one of the 'meta', 'core', or 'site' package hashes in App::CELL::Config), a config parameter (i.e. a string), config value, config file name, and line number.

Let's imagine that the configuration parameter is "FOO_BAR". The function first checks if a key named "FOO_BAR" already exists in the package hash (which is passed into the function as %ARGS{'Dest'}). If there isn't one, it creates that key. If there is one, it leaves it untouched and triggers a warning.

Although the arguments are passed to the function in the form of a PARAMHASH, the function converts them into ordinary private variables. This was necessary to avoid extreme notational ugliness.