Premium Content

Get Premium Content and take advantage of the full potential available in Stories, Links and the Download Area.
Register as a regular user, it's free, and become a community member, share your knowledge or start promoting Cobol in open source.

Topics

User Functions

Older Stories


Welcome to myCobol.net Saturday, 25 November 2017, 10:21 @ CET

Common Block

CoCoS
  • Sunday, 01 March 2015, 12:14 @ CET
  • Contributed by:
  • Views:
    765

Cobol has no global data, i.e. data items that are shared among several program units (subroutines). The only way to pass information is to use the call parameter list. Sometimes this is obfuscating the Cobol text, e.g., when many subroutines share a large set of parameters. In such cases one can use a common block. This is a way to specify that a set of data items should be shared among  subroutines. But in general, the number of common blocks should be minimized.

Using such commonly referenced common block requires a discipline to not overload the contents of such block. Which leads to a few obligatory fields, to be used by any called routine and/or resident program.

First there is a return-status to define. This must be set on exit to oke or error or other needed values, though one does not want to imitate the variable range of the exit status that a *nix shell is offering. We want to keep this minimalistic:

  • 0 = oke,
  • 1 = not-oke,
  • 2 = at-end.

Second there is a control-request to define. This will be set before entry (calling) and specifies the action that the subroutine must perform. But beware! This item has to reflect all possible functions that one's subroutines may ever support. For subroutines that manipulate formats (date conversion, calculations, etc) there seems no need for this item and good code would still use a default value like zero for 'special'. Other subroutines need some state (like file IO) or need a modifier to tell how the function could be performed (again as in file IO for serial/random access or locking features). We want to keep this minimalistic too:

  • 0 = special,
  • 1= read,
  • 2= update,
  • 3 = delete,
  • 4 = insert,
  • 6 = start,
  • 7 = open/initialise,

8 = close/shutdown.

The modifier would specify

  • locking yes/no (negative value / positive value)
  • serial access (1) or random access (2).

This brings us to the follwing code (please note the 'x' in the names to avoid a reserved word):


01 common-block.
 05   return-status     pic x(01).
      88 oke             value "0".
      88 not-oke         value "1".
      88 at-end          value "2".
 05   control-request   pic x(01).
      88 custom          value "0".
      88 readx           value "1".
      88 updatex         value "2".
      88 deletex         value "3".
      88 insertx         value "4".
      88 startx          value "6".
      88 openx           value "7".
      88 closex          value "8".
 05   function-modifier pic s9(01).
      88 serialx         value -1, 1.
      88 randomx         value -2, 2.
      88 locking-mode    value -1, -2.

The following comments are owned by whomever posted them. This site is not responsible for what they say.