16.6 Dynamic Program Generation
In addition to all the dynamic programming concepts discussed in this chapter, which can be used within an ABAP program, we can also generate ABAP programs dynamically. Generating a program dynamically should be used as a last resort if other dynamic programming techniques do not satisfy your requirements.
Dynamic program generation is for expert programmers and should be used with caution; it can cause serious problems if you don’t know what you’re doing. The downsides of programs generated dynamically are that these programs can’t be tested like regular programs and can involve serious security risks.
There are two types of programs that can be generated dynamically:
-
Transient program
A transient program exists temporarily in the internal session memory and can be accessed only from the current internal session. The GENERATE SUBROUTINE POOL itab NAME prog statement is used to generate a temporary subroutine pool program. The internal table itab should be a character type and contains the source code of the program. The prog variable contains the name of the generated program, via which the program can be accessed.
The generated subroutine pool program can contain local classes or subroutines, which can be accessed from outside of the program. If the source code of the program contains any syntax errors, the program isn’t generated.
Listing 16.28 shows sample code to generate a temporary subroutine pool program. In this example, we’re appending the internal table IT_SOURCE with the source code of the subroutine pool program and generating the program using the GENERATE SUBROUTINE POOL statement.DATA: it_source TYPE TABLE OF string,
program TYPE string ,
mesg TYPE string.
APPEND 'PROGRAM.' TO it_source.
APPEND 'FORM subr.' TO it_source.
APPEND 'WRITE / ''This is dynamic subroutine''.' TO it_source.
APPEND 'ENDFORM.' TO it_source.
GENERATE SUBROUTINE POOL it_source NAME program MESSAGE mesg.
IF sy-subrc = 0.
PERFORM subr IN PROGRAM (PROGRAM).
ENDIF. -
Persistent program
Persistent programs exist permanently in the repository and can be accessed just like other programs that are created manually. The INSERT REPORT prog FROM itab statement generates a persistent program. Here, the internal table itab contains the source code of the program, and the prog variable contains the program name.
You should be careful when using this statement: If a program with the same name already exists, it will be overwritten without any warning. To ensure you don’t overwrite existing programs, we recommend checking the database table TRDIR to see if a program with the same name already exists.
Listing 16.29 shows example code to generate a program dynamically. In this example, we’re filling the internal table IT_SOURCE with the source code line by line and then using the INSERT REPORT keyword to generate the program.REPORT ZDYN_PROG.
DATA: it_source TYPE TABLE OF rssource-line.
APPEND 'REPORT ZDYN_EXAMPLE.' TO it_source.
APPEND 'WRITE / ''This program is generated dynamically''.'
TO it_source.
INSERT REPORT 'ZDYN_EXAMPLE' FROM it_source.
When generating programs dynamically, it’s a good idea to create a program statically and use that program as a template to generate new programs. You can use the READ REPORT INTO itab statement to read the source code of an existing program, which can then be used as a template to generate a new program.