In this module, we will explore how to call external programs from within a CL (Control Language) script. This is a crucial skill for integrating various systems and leveraging existing functionalities provided by other programs.

Objectives

By the end of this module, you will:

  • Understand the concept of calling external programs.
  • Learn the syntax and commands used to call external programs.
  • Practice calling external programs with practical examples.
  • Handle errors and return values from external programs.

  1. Introduction to Calling External Programs

Calling external programs allows a CL script to execute other programs, which can be written in different languages or be part of the system's utilities. This capability is essential for:

  • Reusing existing code.
  • Integrating different systems.
  • Automating complex workflows.

  1. Basic Syntax

The primary command used to call an external program in CL is CALL. The syntax is straightforward:

CALL PGM(program_name) PARM(parameter_list)
  • PGM(program_name): Specifies the name of the program to be called.
  • PARM(parameter_list): Specifies the parameters to be passed to the program.

Example

Let's consider a simple example where we call a program named MYPGM with two parameters:

CALL PGM(MYPGM) PARM('param1' 'param2')

  1. Practical Example

Example Program: MYPGM

First, let's create a simple program named MYPGM that takes two parameters and prints them.

// MYPGM.RPGLE
PGM        PARM(&PARAM1 &PARAM2)
DCL        VAR(&PARAM1) TYPE(*CHAR) LEN(10)
DCL        VAR(&PARAM2) TYPE(*CHAR) LEN(10)

SNDPGMMSG  MSG('Parameter 1: ' *CAT &PARAM1)
SNDPGMMSG  MSG('Parameter 2: ' *CAT &PARAM2)

ENDPGM

Calling MYPGM from a CL Script

Now, let's write a CL script to call MYPGM with parameters.

// CALLMYPGM.CLLE
PGM

DCL VAR(&PARM1) TYPE(*CHAR) LEN(10) VALUE('Hello')
DCL VAR(&PARM2) TYPE(*CHAR) LEN(10) VALUE('World')

CALL PGM(MYPGM) PARM(&PARM1 &PARM2)

ENDPGM

Explanation

  1. Declare Variables: We declare two variables &PARM1 and &PARM2 with initial values 'Hello' and 'World'.
  2. Call Program: We use the CALL command to call MYPGM and pass the variables as parameters.

  1. Handling Errors and Return Values

When calling external programs, it's essential to handle potential errors and capture return values. The MONMSG command can be used to monitor for specific messages and handle them accordingly.

Example with Error Handling

// CALLMYPGM_WITH_ERROR_HANDLING.CLLE
PGM

DCL VAR(&PARM1) TYPE(*CHAR) LEN(10) VALUE('Hello')
DCL VAR(&PARM2) TYPE(*CHAR) LEN(10) VALUE('World')

CALL PGM(MYPGM) PARM(&PARM1 &PARM2)
MONMSG MSGID(CPF0000) EXEC(DO)
    SNDPGMMSG MSG('Error occurred while calling MYPGM')
ENDDO

ENDPGM

Explanation

  1. Call Program: We call MYPGM as before.
  2. Monitor for Errors: We use MONMSG to catch any error messages (CPF0000 is a generic message ID for all errors) and execute a block of code if an error occurs.
  3. Send Error Message: If an error occurs, we send a program message indicating the error.

  1. Exercises

Exercise 1: Call an External Program

  1. Create a program named EXTPGM that takes one parameter and prints it.
  2. Write a CL script to call EXTPGM with a parameter value of your choice.

Solution

EXTPGM

// EXTPGM.RPGLE
PGM        PARM(&PARAM)
DCL        VAR(&PARAM) TYPE(*CHAR) LEN(10)

SNDPGMMSG  MSG('Parameter: ' *CAT &PARAM)

ENDPGM

CL Script

// CALLEXTPGM.CLLE
PGM

DCL VAR(&PARM) TYPE(*CHAR) LEN(10) VALUE('TestValue')

CALL PGM(EXTPGM) PARM(&PARM)

ENDPGM

Exercise 2: Error Handling

  1. Modify the EXTPGM program to intentionally cause an error (e.g., divide by zero).
  2. Update the CL script to handle the error and print an appropriate message.

Solution

EXTPGM with Error

// EXTPGM.RPGLE
PGM        PARM(&PARAM)
DCL        VAR(&PARAM) TYPE(*CHAR) LEN(10)
DCL        VAR(&ZERO) TYPE(*DEC) LEN(3 0) VALUE(0)
DCL        VAR(&RESULT) TYPE(*DEC) LEN(3 0)

CHGVAR     VAR(&RESULT) VALUE(1 / &ZERO) // This will cause a divide by zero error

SNDPGMMSG  MSG('Parameter: ' *CAT &PARAM)

ENDPGM

CL Script with Error Handling

// CALLEXTPGM_WITH_ERROR_HANDLING.CLLE
PGM

DCL VAR(&PARM) TYPE(*CHAR) LEN(10) VALUE('TestValue')

CALL PGM(EXTPGM) PARM(&PARM)
MONMSG MSGID(CPF0000) EXEC(DO)
    SNDPGMMSG MSG('Error occurred while calling EXTPGM')
ENDDO

ENDPGM

Conclusion

In this module, we learned how to call external programs from a CL script, pass parameters, and handle errors. This capability is vital for integrating different systems and reusing existing functionalities. In the next module, we will explore interfacing with databases, which will further enhance our ability to create powerful and integrated CL scripts.

© Copyright 2024. All rights reserved