Contents | Parent Topic | Previous Topic | Next Topic
Home | Catalog

# 2.2. app.pl - Application Framework Library

## 2.2.1. Purpose

This library provides a common application framework for Perl scripts.

## 2.2.2. Interface

    require "app.pl";

    $APP_STDIN_ARGS = ...

    $app_lib_dir = ...

    @app_exit_routines = ...

    %app_config = ...

    @app_option_core = ...

    @app_option = ...

    @app_msg_table = ...

    $app_context = ...

    $app_lineno = ...

    %app_msg_index = ...

    $app_log_strm = ...

    $app_product_name = ...

    $app_product_version = ...

    %app_trace_level = ...

    &AppMsg($type, $text, $calltree, $log_only);

    %count =
    &AppMsgCounts($start_index);

    $index =
    &AppMsgNextIndex();

    &AppExit($type, $text, $calltree, $log_only);

    &AppTrace($group, $level, $msg, $log_only);

    $ok =
    &AppInit($arguments, $purpose, $product, $ini_handler);

    &AppPrintUsage();

    %values =
    &AppSectionValues($strs);

    &AppShowParts();

    &AppShowCallTree();

    $app_err =
    &AppProcess($arg_process_fn, $arg_post_process_fn, $default_ext);

## 2.2.3. Description

### 2.2.3.1. Overview

This library provides routines for:

- option processing
- argument processing
- message handling
- exiting a script

In principle, scripts using this library have a consistent user interface and internal structure, making them easier to use and maintain. A typical script looks like:

         require "sdf/app.pl";

         # define configuration
         %app_config = (
                 'inifile',      'abc.ini',
         );

         # define options
         push(@app_option, (
                 'str|STR|my string parameter',
         ));

         # handle options
         &AppInit('file ...', 'do things to files') || &AppExit();

         # my post init stuff. e.g.
         # * act on options
         # * pop leading/trailing argument

         &AppProcess('argProcess');

         # my pre exit stuff. e.g.
         # * print summary information

         &AppExit();

To summarise the framework:

- %app_config is an optional set of configuration parameters
- @app_option is the set of options supported
- AppInit processes options and checks the argument count
- AppProcess processes arguments (i.e. for each one, calls a user defined subroutine typically called argProcess)
- argProcess outputs messages using AppMsg
- AppExit exits and returns an error code to the operating system based on the severity of the messages output via AppMsg

### 2.2.3.2. Option Definitions

Each option is defined by the fields in the table below.

Field | Description
Option | option name
Spec | option specification (type, etc.)
Help | short description of option

The name of the option is used in several ways:

- as the name of the Perl variable (e.g. $help)
- as the name of the long option (e.g. --help)

If short format is enabled (e.g. -h), by default, the first character of the option name is used as the option code. Alternatively, the code to use can be explicitly specified using the Option syntax:

          name";"code

This should be done if the default codes are not unique. To specify an option without a parameter, use the following Spec syntax:

         BOOL

If no Spec field is specified, this is the default. The value of a parameter-less option is 1 if the option if supplied on the command-line.

To specify an option with a parameter, use the following Spec syntax:

         opt_type"-"validation";"initial_value";"default_value

Only the opt_type field is mandatory. The second semi-colon must be present to nominate the parameter as optional. Otherwise, the parameter is assumed to be required. The list of supported opt_types is given in the table below.

Type | Validation | Description
STR | value list | text string
INT | min,max | integer number
NUM | min,max | real number

min and max are optional in the validation of numeric options. Typical STR definitions are given below.

         STR-(qwe,asd,zxc);qwe;asd       # in-lining of codes
         STR-@ok_array;qwe;asd           # array lookup
         STR-keys %ok_hash;qwe;asd       # hash validate
         STR-values %ok_hash;qwe;asd     # hash validate

An array of STR, INT or NUM can be requested by suffixing the type with 'LIST' or 'HASH'. LIST create a normal array and HASH create an associative array in Perl. Array options can be specified multiple times on the command-line and multiple items can be specified at once separated by commas. For a LIST, the values supplied are appended to the current list. For a HASH, values are treated as follows:

- if the format is 'x=y', the result is: $optname{'x'} = 'y'
- otherwise, the result is: $optname{'x'} = 1

In some cases, it is useful to give an array option an initial value but permit the user to 'reset' the array back to empty. This can be done by defining the option as having an empty optional parameter. i.e. selecting the default value of a parameter-optional LIST or HASH does not append the default value - it initialises the array to the default value.

If finer control is required over processing an option, a routine can be nominated to process an option. To do this, use the following Spec syntax:

         ROUTINE"-"routine_name";"initial_value";"default_value

The parameter type is specified by the number of semi-colons:

- 0 - no parameter
- 1 - required parameter
- 2 - optional parameter

The parameter, if any, is passed as a string to the nominated routine.

### 2.2.3.3. Configuration Parameters

The configuration parameters supported are summarised below.

Parameter | Description
inifile | the name of the initialisation file to use, if any
version | the script version
product | the product to which this script belongs, if any
parts | display name & version of components (and exit)
time | display execution time (when exiting)
calltree | display the sequence of function calls causing the exit

There are several ways to set configuration parameters:

1. by initialising %app_config before calling AppInit
2. by defining parameters in the '[Configuration]' section of an initialisation file
3. by setting parameters on the command line (using the . option)
4. for backwards compatibility, certain parameters can be set other ways:
   - version can be set via $VERSION{'PUBLIC'}
   - product can be set via AppInit

### 2.2.3.4. Configuration Data

Configuration data can be stored in ini files and loaded during the initialisation stage (i.e. while AppInit is running). To do this, specify a subroutine as the ini_handler parameter for AppInit. The routine is passed the name of the ini file and an associative array containing the sections (after the standard ones like 'Configuration' have been removed). A warning will be produced for any sections not processed by your routine, so delete each section after you have finished processing it. A sample processing routine is given below.

    sub iniProcess {
        local($fname, *data) = @_;
    #   local();
        local($section);
        local(@my_table, %my_values);

        for $section (sort keys %data) {
            if ($section eq 'MyTable') {
                @my_table = &TableParse($data{$section});
                delete $data{$section};
            }
            elsif ($section eq 'MyValues') {
                %my_values = &AppIniValues($data{$section});
                delete $data{$section};
            }
        }
    }

### 2.2.3.5. Component Descriptions

$APP_STDIN_ARGS is the pseudo argument (default '+') which causes standard input to be processed as a list of arguments. Some scripts may wish to use another symbol (i.e. '+' might be required as a genuine argument), or disable this behaviour altogether. See AppProcess.

$app_path, $app_dir and $app_name are the full pathname, directory and name of the application respectively.

$app_lib_dir is the library directory for this application, i.e. the directory containing configuration files. The default value is the $app_dir. This directory is typically set by searching the Perl library path for the libdir configuration parameter, if any.

@app_exit_routines is the stack of routines to be executed on program termination. If you want a routine to be called on termination (normal and abnormal), push the name of the routine onto this stack. These routines will be executed when AppExit is called. It is thus advisable to ensure that AppExit is NOT called within an exit routine.

%app_config contains the application's configuration parameters. @app_option defines the options supported by the application. The default options are help, out_ext, log_ext and out_dir. To append to these, push your arguments onto the array. For example:

     push(@app_option,
          'report|STR|report file',
     );

If the script will never have a need for 'per file' output or errors, assign @app_option_core to @app_option before appending your script-specific options. For example:

     @app_option = @app_option_core;
     push(@app_option,
          'report|STR|report file',
     );

To obtain a concise description of each option, use the help option with no parameter. Alternatively, detailed help on a given option can be obtained by suppying the option name as a parameter.

By default, output goes to standard output and diagnostics goes to standard error. These rules can be changed by specifying the out_ext and log_ext options respectively (and calling AppProcess to process arguments). If a string is supplied to these options, it is treated as the extension of the file to send things to for each file. If supplied without a parameter, the extensions default to out and log respectively. A minus character (-) or an equals character (=) can be used to indicate standard output or standard error respectively.

By default, output and log files are created in the current directory. To specify the same directory as the input file, specify the out_dir option without an argument. To specify an explicit directory, pass that directory as the argument to the out_dir option.

app_msg_table defines the known message types. Each type is defined by the attributes in the table below.

Attribute | Description
Type | message type name
Severity | application exit code caused by this message
Layout | format of message text

Layout can include the symbols given in the table below.

Symbol | Description
$text | user text
$type | message type
$app_name | application name
$ARGV | current argument name (usually a file name)
$app_context | current "context" (e.g. 'line ')
$. | current line number
$app_lineno | current line number (if $. is 0)

The standard message types are explained in the table below.

Tag | Severity | Description
current object: |  |
object | 0 | general information
warning | 8 | something you should know
error | 16 | something you should fix
abort | 24 | cannot precede processing
whole application: |  |
app | 0 | general information
app_warning | 10 | something you should know
app_error | 18 | something you should fix
fatal | 32 | cannot precede processing
non-user messages: |  |
debug | 0 | debugging diagnostics - ignore
failed | 64 | internal check failed - notify developer

All messages are output to the standard error stream with a newline appended and prefixed as follows:

- object messages by the current object name
- warning, error and abort by current object name, line number and message type
- app messages by the application name
- app_warning messages by the application name and 'warning'
- app_error messages by the application name and 'error'
- fatal messages by the application name and 'fatal'
- debug by application name and 'debug'
- failed by application name and 'internal failure'

Most applications only use fatal, abort, error and warning. fatal is used when an application decides to terminate. (e.g. when an option is illegal.) abort is used when an application decides not be precede any further on the current object (e.g. too many errors encountered). error is used when a serious error is detected in processing the current object. warning is used when a minor error or possible error is detected. Typically, an application continues processing the current object when an error or warning is encountered but errors prevent further passes on the object while warnings do not.

app_context and app_lineno are the context and line number used in error messages. app_lineno is only used if $. is 0.

%app_msg_index is the index into the message table. (Most programmers have no need for this, but it's provided in case someone does want it.)

$app_log_strm is a stream on which all messages are logged to if it's set. If required, this stream is opened and provided by the user.

app_product_name and app_product_version are the application name and version respectively. These are typically set during execution of the AppInit routine.)

app_trace_level is the highest level of trace messages output by AppTrace for each tracing group.

AppMsg outputs a message. The format of the message is determined by the type parameter which should be defined in app_msg_table. If the type is unknown, behaviour is undefined. If calltree is set, a call tree is dumped after the message is output. If log_only is set, the message is only output to the $app_log_strm, if any.

If a message layout includes the current line number ($.) and it is 0, AppMsg uses the dot-version (e.g. ".error") of the message instead.

The messages output via AppMsg influence the exit code returned to the operating system by AppExit. If you wish to influence this but not output a message, specify a type parameter without a text parameter.

AppMsgCounts returns the number of each message type found. If you are interested in the message counts since a particular point in time, a starting index to begin the counting from can be specified.

AppMsgNextIndex returns the next index to be used in the message log. The value returned can be used as the start_index parameter to the AppMsgCounts routine.

AppExit exits the current application. If a message is specified, it is first output via AppMsg. The exit code returned to the operating system is dependent on the messages output by AppMsg. If calltree is set, a call tree is dumped after the message is output. If log_only is set, the message is only output to the $app_log_strm, if any.

AppTrace outputs a trace message if group tracing is supported and for that group, the trace level is >= level. The default group is called user. If log_only is set, the message is only output to the $app_log_strm, if any.

AppInit processes options and checks the argument count for a perl script. The supported options are defined by @app_option. Options must occur before arguments and begin with a - character for the short format or -- for the long format. Option processing is terminated when either an argument or the -- symbol is detected. If an environment variable of the form app_nameOPTS is found, options are first processed from there.

The expected number of arguments is derived from the format of the arguments parameter as illustrated by the table below.

Expected | Format
0 | ""
0 or more | "..."
1 | "file"
1 or more | "file ..."
2 | "source destination"
2 or more | "pattern file ..."
2 or more | "file ... destination"

The pattern "..." is used to detect if a variable number of arguments is permitted. If no arguments are supplied and one or more are expected, then a concise usage message is output. If an application does not require an argument, there is no way to output only a concise usage (use the help option instead). purpose is displayed as part of the usage message. product is an optional parameter. If it is supplied and a product of that name exists in the internal product version lookup table, the product version is included in the usage too. Note that the usage message always includes a script version, regardless of whether a product version is displayed or not.

If AppInit encounters an error, it outputs a usage message and returns 0. Otherwise, it returns 1.

AppPrintUsage outputs the usage header message build during the last call to AppInit. Only the first call to this routine (after AppInit is called) will print the message. This allows programmers to do additional validation after AppInit returns and know that only one usage header message will be output.

AppSectionValues converts an inifile section into a set of name-value pairs.

AppShowParts displays the versions of components making up this application and exits. To support this facility, each library should include a line of the form:

$VERSION{__FILE__} = "x.y"

Strings containing SCCS or RCS stuff have the baggage stripped. For example:

- '@(#) 3.2' is displayed as '3.2'
- '$Revision: 1.27 $' is displayed as '3.3'

AppShowParts is usually called via the '.parts' special help option. However, certain application code might have a need to call it directly.

AppShowCallTree displays the call tree (excluding the call to itself). The routine is usually called indirectly:

- via AppMsg or AppExit (calltree parameter set), or
- via the .calltree special help parameter

Like AppShowComponents, certain application code may wish to call AppShowCallTree directly.

AppProcess processes each argument on the command-line. In particular, it does the following for each argument:

- if the argument is '+', processes each line of standard input as an argument
- if a file matching an argument is not found, but default_ext is supplied and adding that extension results in a file being found, then default_ext is added as an extension to the argument
- echos the argument to standard error if there is more than one
- if $out_ext is set, opens an output file for the current argument and redirects STDOUT to it
- if $log_ext is set, opens a log file for the current argument and redirects STDERR to it
- calls arg_process_fn
- close the output and log files, returning STDOUT and STDERR back to their initial state
- calls arg_post_process_fn, if any

Note that arg_post_process is optional - it is only used in scripts which need to do additional processing on an file after output streams have been closed.

arg_process_fn has the following interface:

         $err = &arg_process_fn($arg)

arg_post_process_fn has the following interface:

         $err = &arg_post_process_fn($arg, $arg_err)

where arg_err is the error code returned by arg_process_fn. AppProcess returns the highest error code it encounters from the user processing functions it calls.

If you need to disable the special meaning of '+', set the APP_STDIN_ARGS configuration constant to an empty string. Likewise, you can change the character used by setting it to another value, although this is not recommended given the consistency implications.

## 2.2.4. Limitations and future directions

On MS-DOS using BigPerl v2 or v3, redirecting STDOUT after AppProcess doesn't work.