NAME

CGI::AppBuilder::Config - Configuration initializer

SYNOPSIS

use CGI::AppBuilder::Config;

my $cg = CGI::AppBuilder::Config->new(
   'ifn', 'my_init.cfg', 'opt', 'vhS:a:');
my $ar = $cg->get_inputs; 

DESCRIPTION

This class provides methods for reading and parsing configuration files.

new (ifn => 'file.cfg', opt => 'hvS:')

This is a inherited method from CGI::AppBuilder. See the same method in CGI::AppBuilder for more details.

get_inputs($ifn, $opt)

Input variables:

$ifn  - input/initial file name. 
$opt  - options for Getopt::Std, for instance 'vhS:a:'

Variables used or routines called:

None

How to use:

my $ar = $self->get_inputs('/tmp/my_init.cfg','vhS:');

Return: ($q, $ar) where $q is the CGI object and $ar is a hash array reference containing parameters from web form, or command line and/or configuration file if specified.

This method performs the following tasks:

1) create a CGI object
2) get input from CGI web form or command line 
3) read initial file if provided
4) merge the two inputs into one hash array

This method uses the following rules:

1) All parameters in the initial file can not be changed through
   command line or web form;
2) The "-S" option in command line can be used to set non-single
   char parameters in the format of 
   -S k1=value1:k2=value2
3) Single char parameters are included only if they are listed
   in $opt input variable.

Some parameters are defined automatically:

script_name - $ENV{SCRIPT_NAME} 
url_dn      - $ENV{HTTP_HOST}
home_url    - http://$ENV{HTTP_HOST}
HomeLoc     - http://$ENV{HTTP_HOST}/
version     - $VERSION
action      - https://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}
encoding    - application/x-www-form-urlencoded
method      - POST
script_name - {home_url}{script_name}

get_opt($opt)

Input variables:

$opt  - option parameter such as 'a:hi:s:S:t:v:'
@ARGV - inputs 

Variables used or routines called:

None.

How to use:

my $opts = 'a:hi:s:S:t:v:';
$self->get_opt($opts);

Return: None

read_init_file($fn, $dvr)

Input variables:

$fn - full path to a file name
$dvr - delay variable replacement
       0 - No (default)
       1 - yes

Variables used or routines called:

eval_variables - replace variables with their values

CGI::AppBuilder::Message
  echo_msg - echo messages

How to use:

my $ar = $self->read_init_file('crop.ini');

Return: a hash array ref

This method reads a configuraton file containing parameters in the format of key=values. Multiple lines is allowed for values as long as the lines after the "key=" line are indented as least with two blanks. For instance:

width = 80
desc  = This is a long
        description about the value
# you can define perl hash araay as well
msg = {
  101 => "msg 101",
  102 => "msg 102"
  }
# you can use variable as well
js_var = /my/js/var_file.js
js_src = /my/first/js/prg.js,$js_var
# a comma (,) after sharp (#) make it not a comment
my_sql = select sid, serial#,username from v\$session; 

This will create a hash array of

$ar->{width} = 80
$ar->{desc}  = "This is a long description about the value"
$ar->{msg}   = {101=>"msg 101",102=>"msg 102"}
$ar->{js_var}= "/my/js/var_file.js";
$ar->{js_src}= "/my/first/js/prg.js,/my/js/var_file.js";

eval_variables($cfg, $hr)

Input variables:

$cfg - a hash array ref containing variable names
$hr  - a hash array ref contianing  varliable values

Variables used or routines called:

eval_named_var - get named variables' values
eval_var       - get variables' values

How to use:

my $mr = $self->eval_variables($cfg, $hr);

Return: a hash or hash ref.

This method evaluates the configuration hash and replace variable names with their values up to 5 levels of nested variables. For instance, you have the following configuration hash:

my $cfg = { a=>10, b=>"$a+2", c=>"2*($b)", d=>"$c-1", 
            result=>"3*($d)" }
my $mk = $self->eval_variables($cfg);   

This will result $cfg to

a = 10
b = 10+2
c = 2*(10+2)
d = 2*(10+2)-1
result = 3*(2*(10+2)-1)

eval_var($cfg, $hr)

Input variables:

$cfg - a hash ref containing variable names
$hr  - a hash ref which will be used to search for values 

Variables used or routines called:

None

How to use:

 my $cfg = {first_name=>'John', last_name=>'Smith',
    full_name => "\$first_name \$last_name",
    addr1=>"111 Main Street",
    city=>"Philadelphia", zip_code=>"19102",
    address => "\$addr1, \$city, PA \$zip_code",
    contact=>"\$full_name <address>\$address</address> \$logo",
    };
 my $hr = { logo => 'http://mydomain.com/images/logo.gif', };
 my $p1 = $self->eval_var($cfg, $hr);
 my $p2 = $self->eval_var($cfg, $hr);
 # The first pass will get full_name, address replaced with values
 # but leave contact with variable names in it.
 # The second pass will get first_name, last_name, and address in
 # contact replaced with their values. 

Return: a hash or hash ref

This method evaluates the variable names contained in a configuration hash and replace the variable names with their values.

eval_named_var($hr, $vn, $sr)

Input variables:

$hr - a hash array ref containing variable names
$vn - variable name default to 'ENV' 
$sr - source hash ref. If omitted, {%$vn} will be used.

Variables used or routines called:

None

How to use:

my %ENV = (HTTP_HOST=>'testdomain.com:8000',USER=>'htu');
my $hr  = {first_name=>'John', last_name=>'Smith'};
my $cfg = { hh=>'$ENV{HTTP_HOST}',usr=>'$ENV{USER}',
           fn=>'$hr{first_name}', ln=>'$hr{last_name}',
         };
my $p1 = $self->eval_named_var($cfg, 'ENV');
# the first pass will get 
#   $cfg->{hh}  = 'testdomain.com:8000'
#   $cfg->{usr} = 'htu'
my $p2 = $self->eval_named_var($cfg, 'hr', $hr);
# the second pass will get 
#   $cfg->{fn}  = 'John'
#   $cfg->{ln}  = 'Smith'

Return: a hash or hash ref

This method evaluates the variable names contained in a configuration hash and replace the variable names with their values.

read_cfg_file($fn,$ot, $fs)

Input variables:

$fn - full path to a file name
$ot - output array type: A(array) or H(hash)
$fs - field separator, default to vertical bar (|)

Variables used or routines called:

CGI::AppBuilder::Message
  echo_msg  - display message

How to use:

my $arf = $self->read_cfg_file('crop.cfg', 'H');

Return: an array or hash array ref containing (${$arf}[$i]{$itm}, ${$arf}[$i][$j];

This method reads a configuraton file containing delimited fields. It looks a line starting with '#CN:' for column names. If it finds the line, it uses to define the first row in the array or use the column names as keys in the hash array.

The default output type is A(array). It will read the field names into the first row ([0][0]~[0][n]). If output array type is hash, then it uses the columns name as keys such as ${$arf}[$i]{key}. If it does not find '#CN:' line, it will use 'FD001' ~ 'FD###' as keys.

#Form: fm1
#CN: Step|VarName|DispName|Action|Description				
0.0|t1|Title||CROP Worksheet				

HISTORY

  • Version 0.10

    This version extracts these methods from CGI::Getopt class: get_inputs, read_init_file, and read_cfg_file.

    0.11 Inherited the new constructor from CGI::AppBuilder.
    0.12 Added eval_var, eval_named_var, eval_variables and
         modified read_init_file method. 
    04/29/2010 (htu) - added get_opt sub
  • Version 0.20

SEE ALSO (some of docs that I check often)

Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, CGI::AppBuilder::Message

AUTHOR

Copyright (c) 2005 Hanming Tu. All rights reserved.

This package is free software and is provided "as is" without express or implied warranty. It may be used, redistributed and/or modified under the terms of the Perl Artistic License (see http://www.perl.com/perl/misc/Artistic.html)

1 POD Error

The following errors were encountered while parsing the POD:

Around line 742:

You forgot a '=back' before '=head1'