[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

As an alternative to prompting, each file assigned to a magnetic tape can have its associated tape device defined through a shell environment variable. The name of this environment variable is the concatenation of COBOL_TAPE_ and the base of the file name used in the COBOL program. The value of this environment variable is the name of the desired tape device. The environment variable needed in Example 6-10 to assign the MARCH.311 file to tape device /dev/rmt0a is:


 % setenv COBOL_TAPE_MARCH /dev/rmt0a     <>

Establishing File Names with ASSIGN and VALUE OF ID

If the file specification is subject to change, you can use a partial file specification in the ASSIGN clause and complete it by using the optional VALUE OF ID clause of the FD entry. In the VALUE OF ID clause, you can specify a nonnumeric literal or an alphanumeric WORKING-STORAGE item to supplement the file specification.

VALUE OF ID can complete a file name specified in ASSIGN TO:


  ASSIGN TO "filename"
  VALUE OF ID ".ext"

In the above example, OPEN would create a file with the name "filename.ext".

VALUE OF ID can override a file name specified in ASSIGN TO:


  ASSIGN TO "oldname"
  VALUE OF "newname"

In the above example, OPEN would create a file with the name newname.

VALUE OF ID can be a directory/device specification and ASSIGN TO can provide the file name, as in the following example:


  ASSIGN TO "filename.dat"
  VALUE OF ID "/usr/"

       or

  ASSIGN TO "filename"
  VALUE OF ID "DISK:[DIRECTORY]"

On OpenVMS, with this code OPEN would create a file with the name DISK:[DIRECTORY]FILENAME.DAT. <>

On Tru64 UNIX, with this code OPEN would create a file with the name "/usr/filename.dat". <>

Establishing Device and File Independence with Logical Names on OpenVMS

On OpenVMS, logical names let you write programs that are device and file independent and provide a brief way to refer to frequently used files.

You can assign logical names with the ASSIGN command. When you assign a logical name, the logical name and its equivalence name (the name of the actual file or device) are placed in one of three logical name tables; the choice depends on whether they are assigned for the current process, on the group level, or on a systemwide basis. Refer to the OpenVMS DCL Dictionary for more information on DCL and a description of logical name tables.

To translate a logical name, the system searches the three tables in this order: (1) process, (2) group, (3) system. Therefore, you can override a systemwide logical name by defining it for your group or process.

Logical name translation is a recursive procedure: when the system translates a logical name, it uses the equivalence name as the argument for another logical name translation. It continues in this way until it cannot translate the equivalence name.

Assume that your program updates monthly sales files (for example, JAN.DAT, FEB.DAT, MAR.DAT, and so forth). Your SELECT statement could look like either of these:


SELECT SALES-FILE ASSIGN TO "MOSLS"

SELECT SALES-FILE ASSIGN TO MOSLS

To update the January sales file, you can use this ASSIGN command to equate the equivalence name JAN.DAT with the logical name MOSLS:


$ ASSIGN JAN.DAT MOSLS

To update the February sales file, you can use this ASSIGN command:


$ ASSIGN FEB.DAT MOSLS

In the same way, all programs that access the monthly sales file can use the logical name MOSLS.

To disassociate the relationship between the file and the logical name, you can use this DEASSIGN command:


$ DEASSIGN MOSLS

If MOSLS is not set as a logical name, the system uses it as a file specification and looks for a file named MOSLS.DAT. <>

Using Environment Variables for File Specification on Tru64 UNIX

On Tru64 UNIX, environment variables can be used as aliases for file specification at run time. File name resolution follows these rules:

  • Use contents of the ASSIGN TO clause or VALUE OF ID clause to find a match against an environment variable.
  • If a match is found, substitute the value of the environment variable in the construction of the file specification.
  • If a match was not found, take the file name as specified.

On Tru64 UNIX, you can also use the literal or alphanumeric item to specify a run-time environment variable set. Refer to setenv(3) in the reference page. <>

The program in Example 6-11 and the commands that follow it illustrate how to use the ASSIGN TO clause in conjunction with an environment variable or logical name.

Example 6-11 Using Environment Variables (Tru64 UNIX) or Logical Names (OpenVMS) for File Specification

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ENVVAR-EXAMPLE.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F-DISK ASSIGN TO "MYENV".
       DATA DIVISION.
       FILE SECTION.
       FD  F-DISK.
       01  DAT-RECORD PIC X(100).

       PROCEDURE DIVISION.
       P0. OPEN OUTPUT F-DISK.
           CLOSE F-DISK.

       PE. STOP RUN.
       END PROGRAM ENVVAR-EXAMPLE.

       On Tru64 UNIX, set an environment variable as follows:

       % cobol -o envtest envvar-example.cob
       % setenv MYENV hello.dat
       % envtest
       % ls *.dat
       hello.dat
       % unsetenv MYENV
       % envtest
       % ls MY*
       MYENV   <>

Setting environment variables at run time can help in moving applications between OpenVMS Alpha or OpenVMS I64 and Tru64 UNIX platforms without having to modify their source COBOL programs. You can define environment variables that access files in a way similar to that in which you access files using logical names on OpenVMS systems. Thus, in Example 6-11, the program is applicable to either Tru64 UNIX or to OpenVMS, because MYENV can refer to an environment variable or to a logical name.

Example 6-12 is another program that can be used on either system, depending on the definition at system level of an environment variable or logical name, as appropriate.

Example 6-12 Using Environment Variables

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ENVVAR-EXAMPLE2.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F-DISK ASSIGN TO "SYS$SCRATCH:envtest.dat".
       DATA DIVISION.
       FILE SECTION.
       FD  F-DISK
           VALUE OF ID "SYS$DISK:".
       01  DAT-RECORD PIC X(100).
       PROCEDURE DIVISION.
       P0. OPEN OUTPUT F-DISK.
           CLOSE F-DISK.
       PE. STOP RUN.
       END PROGRAM ENVVAR-EXAMPLE2.

Example 6-12, on OpenVMS, would produce a file with the name "ENVTEST.DAT". On Tru64 UNIX, "SYS$SCRATCH:" has no meaning because it is a OpenVMS logical. OpenVMS logicals are not defined on Tru64 UNIX. However, the "SYS$SCRATCH:" in the ASSIGN clause can be defined as an environment variable with the following command:


  % setenv 'SYS$SCRATCH:' ./

This would make "SYS$SCRATCH" point to the home directory. This can be used for any OpenVMS logicals used in the HP COBOL source. When you declare an environment variable you should be careful to match the case of what is in the HP COBOL source with the setenv(3) line. <>


Previous Next Contents Index