NAME

CreateX - Routines that help to write CreateX.pl scripts

SYNOPSIS

use CreateX;

($app,$ioc) = app_ioc(ARGV[0]);

$fh = std_open_target $app, $ioc, "substitutions";

with_target_file {
  my $fh = shift;
  #...use $fh for writing...
} $app, $ioc, "substitutions";

std_create_subst_and_req(ARGV[0],
  sub {
    my ($app,$ioc,$fh) = @_;
    #...write stuff into subst file $fh
  }
  sub {
    my ($app,$ioc,$fh) = @_;
    #...write stuff into req file $fh
  }
);

std_create_subst(ARGV[0],
  sub {
    my ($app,$ioc,$fh) = @_;
    #...write stuff into subst file $fh
  }
);

write_subst_line($fh, "$name=\"$value\"");

sub write_subst_line_from_hashref($fh, {$name=>$value});

write_template($fh,"xyz.template",
  sub {
    my $fh = shift;
    print $fh " { NAME=DEVICE1, ADDR=0 }\n";
    print $fh " { NAME=DEVICE2, ADDR=1 }\n";
  }
);

perform_query($query,
  sub {
    my ($row,$colnames) = @_;
    $row->{MY_NEW_COLUMN} = "a value for my new column";
    print $fh join(",", map("$_=\"$row->{$_}\"", @$colnames));
  },
  sub {
    my ($colnames) = @_;
    push(@$colnames, "MY_NEW_COLUMN");
  }
);

write_template_sql($fh,$template,$query,
  sub {
    my ($row,$colnames) = @_;
    $row->{MY_NEW_COLUMN} = "a value for my new column";
    print $fh join(",", map("$_=\"$row->{$_}\"", @$colnames));
  },
  sub {
    my ($colnames) = @_;
    push(@$colnames, "MY_NEW_COLUMN");
  }
);

DESCRIPTION

app_ioc EXPR

Split argument string into exactly two '.'-separated parts. Useful for CreateSubst scipts, where the first argument is often "appname.iocname".

std_open_target STRING STRING STRING

Open a target file for writing and return the file handle. The rest of the arguments are (1) an application name (2) an optional IOC name, and (3) the file name suffix. The file name to be opened is constructed by joining these parts with dots in between. If an IOC name is not part of teh file name, an undefined value must be given instead. The routine also prints a message indicating that the a file of the given type (=suffix) is created for the given application name and IOC name.

with_target_file BLOCK ARGS

Execute the first argument (a code block) with an open file handle as first argument. The handle is opened using the remaining arguments as parameters to "std_open_target". The handle is closed after the code block has been executed.

std_create_subst_and_req STRING SUB SUB

Standard 'main' routine to create a substitution and optionally a request file. The first argument is a string of the form "application.ioc", usually $ARGV[0]. The second and third arguments must be subroutines that perform the actual work of writing some stuff to the substition (2nd arg) resp. request file (3rd arg, optional).

The passed subroutines in turn both get passed the following four arguments:

1. application name
2. ioc name
3. file handle, opened via L</with_target_file>
std_create_subst STRING SUB

Standard 'main' routine to create a substitution file. This is just calls "std_create_subst_and_req" with the given two arguments.

perform_query DB_HANDLE QUERY ROW_HANDLER COLNAME_HANDLER

Generic routine to handle a single row returned by a SQL query on a database handle. It is parameterized on database handle, SQL query, and row/colname handler routines, that perform the real work. The arguments to perform_query are:

1. sql query (a string)
2. a row handler routine that takes
   1. a reference to a hash containing name/value pairs
      (by parsing csv output of pg_request.py), and
   2. a reference to an array of column names

3. a colname handler routine that takes a reference to an
   array of column names
write_subst_line FILE STRING

Write one instantiation line inside some file-section of a substitution file.

write_subst_line_from_hashref FILE HASH

Write one instantiation line inside some file-section of a substitution file by using the hash keys as macro names and the hash values as macro values.

write_template FILE STRING SUB

Write a complete file-section in a substitution file. The arguments are:

1. file handle to write to
2. name of the template file to instantiate
3. procedure that writes the substitution lines
write_template_sql

Write a complete file-section in a substitution file, based on sql query. The arguments are:

1: file handle to write to
2: name of the template file to instantiate
3: database handle to perform query on
4: sql query
5: [optional] reference to a row patch routine that takes
   1: a reference to a hash containing name/value pairs
      (by parsing csv output of pg_request.py), and
   2: a reference to an array of column names
6: [optional] reference to a colnames patch routine that takes
   a reference to an array of colnames