Basic Program Structure

CobolScript® Basic Program Structure

C

obolScript does not explicitly require a fixed framework of divisions, sections, and modules in every program. Instead, CobolScript variable definitions, file descriptions, and procedural statements can be placed in any location in a program and be considered valid. However, basic COBOL program framework is supported by CobolScript for former COBOL programmers to use, if desired. This appendix defines the components of that framework, for those programmers that wish to follow older coding conventions. Again, all header sentences are optional, and variable definitions and code statements may still appear anywhere within a program (except within the Identification and Environment divisions, if they are included in your program).

CobolScript Program Templates

The code below is a basic, complete CobolScript program template. CobolScript enforces conventions for code placement and commenting, so the column position, or offset, of your code is relevant; comments must begin with an asterisk in the seventh column, and all code statements must begin after the seventh column. (Column 7 is the seventh character position from the left edge of the text file that contains your code.) Note the lack of division and section headers, and the FD statement or variable definition placed between procedural statements:

* Comments begin with an * in the 7th column.

*********************************************

FD `<filename>` RECORD IS <record-size-in-bytes> BYTES.

1 <variable> PIC <picture-clause> VALUE `<value>`.

1 <group-item-name>.

5 <variable> PIC <picture-clause> VALUE `<value>`.

1 <occurs-variable> OCCURS <dimension> TIMES PIC <picture-clause>.

<procedural statement>.

.

.

<FD statement or variable definition>.

<procedural statement>.

.

.

<STOP RUN>.

In contrast, the following code is a template of a CobolScript program that makes use of COBOL division and section headers:

IDENTIFICATION DIVISION.

*************************************************************

* Comment lines are any lines with an asterisk in column 7 *

*************************************************************

PROGRAM-ID. <this-filename>.

AUTHOR. <authors-name>.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE COMPUTER. <source-computer-type>.

OBJECT COMPUTER. <object-computer-type>.

DATA DIVISION.

FILE SECTION.

FD <filename> RECORD IS <record-size-in-bytes> BYTES.

WORKING-STORAGE SECTION.

01 <data-item-name> PIC <picture-clause> VALUE `<value>`.

01 <group-item-name>.

05 <data-item-name> PIC <picture-clause> VALUE `<value>`.

01 <table-name> OCCURS <table-size> TIMES.

05 <data-item-name> PIC <picture-clause> VALUE `<value>`.

PROCEDURE DIVISION.

<module-name>.

<procedural statement>.

.

.

<STOP RUN | GOBACK>.

<module-name>.

<procedural statement>.

.

.

.

.

Division headers, section headers, and module names, when used, must begin in column 8 or higher, just like code statements.

There are four program division headers, or sentences, that can optionally be specified in CobolScript programs. These headers indicate the beginning of a particular division to the CobolScript engine. The division headers are:

· The Identification Division sentence;

· The Environment Division sentence;

· The Data Division sentence;

· The Procedure Division sentence.

The entire content of the Identification and Environment divisions is treated as informational only by the CobolScript engine. Thus, no program logic or variable definitions can be specified in either of these divisions. Instead, they are used to describe the program and the machines it will be executed on.

The Data division normally contains file and variable information. File Description statements (FDs), COPY statements that load variable-only copybooks, and variable definitions may all be located within the Data Division.

The Procedure division contains the program logic. In COBOL, this division is restricted to only allow code statements, but in CobolScript, variable definitions and file descriptions may also be included.

Although the division style of program layout may strike some programmers today as unwieldy, the existence of the divisions has a historical basis, and arguably still has merit. Originally, COBOL was designed to follow the layout of technical specifications, and the four program divisions were meant to match the divisions of the specifications. This layout is familiar to COBOL programmers, and it does lend itself well to maintenance and documentation.

Each program division begins with its own sentence, called a division header, which is just the name of the division, followed by a period, as in:

IDENTIFICATION DIVISION.

Some of the divisions can be further divided into sections, which have their own section header sentences. Convention dictates that the division and section header sentences are in all capital letters, as in the example above and in the program template on the previous page, but this is not a requirement in CobolScript.

Either the GOBACK or STOP RUN command is required as the last statement in the first module of a program, if there is more than one module in your program. These statements both terminate the execution of the program and prevent control from ‘falling through’ to subsequent modules.

Note that it is acceptable to exclude certain division headers from a program but not others, if division headers are being used. For instance, the Identification division sentence can be excluded from a program that uses the Environment, Data, and Procedure division sentences, but the Procedure division sentence should not be excluded from a program that includes the Identification division sentence.

For this reason, we recommend that you either use all of the division headers, or else completely exclude them from your programs, in order to avoid confusion. If you are a programmer who is accustomed to languages like C, you will probably be most comfortable in completely excluding division and section headers from your programs. If this is the case, you may opt to skip the remainder of this appendix.

Each of the divisions is described below, in the order that their header sentences should appear within a program, if you choose to use them.

The Identification Division

The Identification Division contains descriptive information about the program. This information is essentially for documentation purposes; because of this, and because the Identification Division is the first division of every program, it is also an excellent place to put a comment block that gives an overview of the program.

The Identification Division contains only four sentences: The PROGRAM-ID sentence and its argument sentence, and the AUTHOR sentence and its argument sentence. The PROGRAM-ID sentence and its argument describe the name of the program, as in:

PROGRAM-ID. TEST.CBL.

The AUTHOR sentence and its argument name the creator of the program, as in:

AUTHOR. B. SHAKE-SPEARE.

The Identification Division is strictly an optional component of CobolScript programs.

Here’s what a complete Identification Division might look like:

IDENTIFICATION DIVISION. *************************************************************

* Comment lines are any lines with an asterisk in column 7 *

*************************************************************

PROGRAM-ID. TEST.CBL.

AUTHOR. DESKWARE.

The Environment Division

The Environment Division contains descriptive information about the computer that the program was developed on, and the computer that the code will be executed on. Like the Identification Division, the Environment Division is solely for informational purposes in CobolScript and is entirely optional.

In CobolScript, the Environment Division contains a single section called the Configuration Section. The Configuration Section contains four sentences: The SOURCE COMPUTER and its argument sentence, and the OBJECT COMPUTER and its argument sentence. The SOURCE COMPUTER describes the environment the source was developed on, as in:

SOURCE COMPUTER. FreeBSD.

The OBJECT COMPUTER describes the execution environment for this program, as in:

OBJECT COMPUTER. LINUX.

The Input-Output Section (a COBOL section) is not required or supported in CobolScript.

Here’s what a complete Environment Division could look like:

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE COMPUTER. WinNT.

OBJECT COMPUTER. FreeBSD.

The Data Division

The Data Division is the division where files may be described and variables defined.

Describing Files

The File Section of the Data Division is an optional section header that indicates where file description (FD) statements will be located. If you prefer, you can also place your record variable definitions in the File Section. The syntax of the FD statement is described in the Data and Copybook Files section of Chapter 3, CobolScript Language Constructs.

A File Section with one file description sentence and one record definition looks like this:

FILE SECTION.

FD `info.txt` RECORD IS 500 BYTES.

1 info_record.

5 ir_cust_name PIC X(18).

5 ir_free_text PIC X(480).

Note that specifying the File Section header does not preclude you from placing FD statements or record definitions elsewhere in your program.

Defining Variables

The Working-Storage Section of the Data Division is an optional section header that indicates where variables definitions are to be placed. Here’s an example Working-Storage Section:

WORKING-STORAGE SECTION.

1 text_input PIC X(40).

1 order_info.

2 cust_info.

3 name PIC X(12).

3 loc PIC $,999.99.

2 order_amt PIC 99.

1 input_val PIC X(25).

Each CobolScript variable definition, whether group item or elementary item, has a level number and variable name, and elementary items also have picture clauses that define the variable’s length. Variable definitions are described in detail in the Variables section of Chapter 3, CobolScript Language Constructs, and picture clause formats in Appendix E, CobolScript Picture Clauses.

For a more detailed explanation of level numbers and how to use them to manipulate data see Chapter 8, Other Advanced Programming Techniques Using CobolScript.

As with the File Section header, specifying the Working-Storage Section header does not preclude you from placing variable and record definitions elsewhere in your program.

Here’s what one version of a complete Data Division might look like:

DATA DIVISION.

FILE SECTION.

FD `test.dat` RECORD IS 500 BYTES.

1 info_record.

5 ir_cust_name PIC X(18).

5 ir_free_text PIC X(480).

WORKING-STORAGE SECTION.

1 text_input PIC X(40).

1 order_info.

2 cust_info.

3 name PIC X(12).

3 loc PIC $,999.99.

2 order_amt PIC 99.

1 component_1 OCCURS 10 TIMES PIC 99.

The Procedure Division

The Procedure Division is where the logic of your program is located. Since CobolScript code is executed sequentially, the first line of code in the Procedure Division of a CobolScript program is the line that will be performed first. Good programming practice warrants the use of modules (also known as paragraphs) however, so your first code sentence after the Procedure Division sentence would normally be the name of your first (parent) module. This is conventionally a name like MAIN, so that the first two lines of the Procedure Division might be:

PROCEDURE DIVISION.

MAIN.

This first paragraph is unique in that it should end with either the STOP RUN or GOBACK statement. These statements terminate the control flow of the program and prevent subsequent paragraphs from sequentially executing. A complete main module could look like this:

MAIN.

DISPLAY `This is a test`.

PERFORM MODULE-1.

STOP RUN.

Code modularity is achieved by naming other paragraphs with paragraph header sentences, and then calling these other named paragraphs with the PERFORM statement, as in the PERFORM above. A paragraph header sentence is just a module name, with no spaces between characters, and a period following the name. Like MAIN, which is a paragraph header sentence, the code for a module begins with the line that follows the header sentence.

A particular paragraph ends where the subsequent one begins, i.e., immediately prior to the next paragraph header sentence. In the following example, the module MODULE-1 ends with the line prior to MODULE-2 (the MOVE statement) and MODULE-2 ends after the DISPLAY statement, since this is the last line of code in this particular program.

MODULE-1.

PERFORM MODULE-2.

MOVE var_1 TO var_2.

MODULE-2.

DISPLAY `This is a test from MODULE-2`.

The entire Procedure Division in this example looks like this:

PROCEDURE DIVISION.

MAIN.

DISPLAY `This is a test`.

PERFORM MODULE-1.

STOP RUN.

MODULE-1.

PERFORM MODULE-2.

MOVE var_1 TO var_2.

MODULE-2.

DISPLAY `This is a test from MODULE-2`.

For a detailed description of how to code in a modular fashion and how to use modules, see Chapter 8, Other Advanced Programming Techniques Using CobolScript.