What’s New in V5R4 COBOL?
March 29, 2006 Hey, Ted
I enjoyed the V5R4 RPG and CL enhancements articles. I can’t wait to use subroutines to clean up code in CL programs. Also, the RPG and SQL tips and techniques are great. But those of us who labor in the COBOL fields could use a little help.
It’s taken me some time to get to it, Thom, but thanks to Philip Mawby, of IBM‘s Toronto software labs, I have the information you need. I’ll hit the high points. Consult the V5R4 COBOL reference for more details.
In V5R4, COBOL stays ahead of RPG in the area of XML support. COBOL learned to read XML in V5R3, but RPG had to wait until V5R4. Now COBOL learns to write XML.
The command to write XML is called XML GENERATE. It has two forms, depending on whether you want to place the generated XML into a variable or an IFS file. Here’s an untested example that gives an idea of how this works.
First, consider the following source code from the data and procedure divisions.
Data division. 01 EmployeeRec. 03 Name pic X(25). 03 Badge pic S9(5). 03 MailAddress. 05 Street pic X(20). 05 City pic X(12). 05 State pic X(2). 03 Personal. 05 MaritalStatus pic X. 05 BirthDate pic X(8). 01 XMLMsg pic X(1000). 01 IFSFile pic x(50) value "/home/mydir/SomeIFSFile.xml". Procedure division. * Place the generated XML into a variable. XML GENERATE XMLMsg FROM EmployeeRec END-XML. * Place the generated XML into an IFS file. XML GENERATE FILE-STREAM IFSFile FROM EmployeeRec END-XML.
Both commands generate XML from the data currently loaded in the EmployeeRec structure. The first XML GENERATE command stores the XML in the XMLMsg variable. The second stores the XML in IFS file SomeIFSFile.xml. In either case, the generated XML looks something like this:
<EmployeeRec> <Name>Joe Smith</Name> <Badge>12345</Badge> <MailAddress> <Street>101 Main St</Street> <City>Lost Angeles</City> <State>NY</State> </MailAddress> <Personal> <MaritalStatus>M</MaritalStatus> <BirthDate>19010203</BirthDate> </Personal> </EmployeeRec>
XML GENERATE has some optional clauses you may be interested in. COUNT IN stores the number of generated XML characters in a variable. ON EXCEPTION and NOT ON EXCEPTION allow the programmer to include code that is to be executed if the XML generation fails or succeeds.
After XML GENERATE executes, you may check the XML-CODE special register to determine failure or success. A value of zero means that the generation was successful.
Conversion of National Data
IBM has added two new intrinsic functions to aid in translating national data.
The NATIONAL-OF function converts alphabetic, alphanumeric, or DBCS to national data of CCSID 13488. NATIONAL-OF accepts one or two arguments. The first argument is the source string–the alphabetic, alphanumeric, or DBCS string that is to be translated.
If the second argument is a one-character national literal or variable, this argument specifies the character to be substituted when a character in the source string cannot be translated into a national character. The default substitution character is X’FFFD’.
If the second argument is an integer, this argument indicates the CCSID of the source string. If there is no second argument, the system assumes the source string is in the format of the CCSID in effect during compilation. If CCSID 65535 was in effect at compilation, CCSID 37 is used by default.
The DISPLAY-OF function converts national to alphanumeric. It accepts one or two arguments. The first is the source string, which must be national data.
If the second argument is a one-character alphabetic, alphanumeric, or DBCS literal or variable, this argument specifies the character to be substituted when a national character in the source string cannot be translated into a character of the target CCSID.
If the second argument is an integer, this argument indicates the CCSID of the target string. If there is no second argument, the system assumes the target string is in the format of the CCSID in effect during compilation. If CCSID 65535 was in effect at compilation, CCSID 37 is used by default.
The following example from the COBOL reference illustrates both functions. This example converts an EBCDIC string to ASCII in two steps. That is, first NATIONAL-OF converts EBCDIC data of CCSID 1140 to national. Then DISPLAY-OF converts national to ASCII (CCSID 819).
77 EBCDIC-CCSID PIC 9(4) BINARY VALUE 1140. 77 ASCII-CCSID PIC 9(4) BINARY VALUE 819. 77 Input-EBCDIC PIC X(80). 77 ASCII-Output PIC X(80). Move Function Display-of (Function National-of (Input-EBCDIC EBCDIC-CCSID) ASCII-CCSID ) to ASCII-output.
V5R4 introduces three functions that remove leading and/or trailing characters from strings. The TRIML (trim left) function removes leading characters, while TRIMR (trim right) removes trailing characters. TRIM removes both leading and trailing characters. The characters to be removed are typically blanks, but other characters may be stripped away as well. The source string is not affected by the operation.
All of these functions take two arguments. The first is the source string, the one to be trimmed. The second argument lists the characters to be removed. If the second argument is not specified, the character list is assumed to consist of one blank. The system will remove characters from the beginning or end of the source string until it finds a character that is not in the character list.
In the following example, TEXTF contains an edited numeric amount. The TRIML function removes leading blanks and dollar signs, storing the string 1,234.56 into PRICE.
data division. working-storage section. 01 TextF pic x(80). 01 Price pic X(12). procedure division. main-logic. move " $1,234.56 " to TextF. move function TrimL(TextF, "$ ") to price.
IBM has added COBOL support for null-terminated non-numeric literals in V5R4. A null-terminated literal looks like an alphanumeric literal with a leading Z prefix. Here’s an example that displays the value of environment variable XYZ_DATA.
process nomonoprc identification division. program-id. cob02. environment division. data division. working-storage section. 01 EnvVar pic x(12) value z"XYZ_DATA". 01 EnvVarValue pic X(50). 01 ValuePtr usage pointer. linkage section. 01 EnvVarParm pic X(50). procedure division. main-logic. call linkage type is procedure "getenv" using EnvVar returning ValuePtr. if ValuePtr = null move "*NONE" to EnvVarValue else set address of EnvVarParm to ValuePtr move EnvVarParm to EnvVarValue end-if. display EnvVarValue. goback.
The getenv API requires that the name of the environment variable be passed as a null-terminated value. In this case, the VALUE clause of the definition of EnvVar stores the null-terminated name of the environment variable.
New Compiler Option
There is one new compiler option that may be used with the Create Bound COBOL Program (CRTBNDCBL) and Create COBOL Module (CRTCBLMOD) commands. You may specify *NOCOMPRESSDBG or *COMPRESSDBG to control the amount of information generated during compilation with DBGVIEW(*LIST) or DBGVIEW(*ALL) in effect.
RELATED STORIES AND RESOURCES