ABAP Keyword Documentation → ABAP − Reference → program editing → Dynamic Program Editing → ABAP Source Code
GENERATE SUBROUTINE POOL
Other versions: 7.31 | 7.40 | 7.54
Syntax
GENERATE SUBROUTINE POOL itab NAME prog [error_handling].
Effect
This statement generates a temporary
subroutine pool. The source code of the subroutine pool is taken from the internal table itab
. The generated subroutine pool is stored internally in the current
internal session.
The eight-character name of the temporary subroutine pool is assigned to the variable prog
. The following can be specified for prog
:
- An existing character-like variable
-
An inline declaration
DATA(var)
, where a variable of type PROGNAME is declared.
For itab
, only a standard table without
secondary table keys
is permitted. The row type of itab
must be character-like. A line of source
code in itab
can have no more than 255 characters (if the row type has a
fixed length, trailing blanks are ignored). In an internal session, a maximum of 36 temporary subroutine pools can be created.
If the source code in itab
has a syntax error, the subroutine pool is not
generated and initialized using prog
. The addition
error_handling
can be used to analyze syntax errors and generation errors. For the syntax check, the switch configuration of
Switch Framework is used in the state it had when the current transaction was called.
If an exception is raised when the subroutine pool is generated, the runtime error is handled internally so that no programs are terminated. Instead, sy-subrc
is set to the value 8. However, there is still a
database rollback and the corresponding
short dump is saved as normal.
The addition SHORTDUMP-ID
can be used to determine the ID of the runtime error.
In the source code of the subroutine pool, subroutines can be called from all programs that are loaded
in the same internal session by specifying the program name prog
using the
statement PERFORM
. When a subroutine
is called for the first time in the subroutine pool, this is loaded into the internal session, and the
event LOAD-OF-PROGRAM
is raised.
System Fields
sy-subrc | Meaning |
---|---|
0 (means: | Generation was successful. |
4 | The source code contains a syntax error. |
8 | A generation error occurred The resulting runtime error was handled internally. |
If a runtime error occurs during the generation process (sy-subrc
has the value 8), a database rollback is executed as usual.
Programming Guideline
Security Note
If used wrongly, dynamic programming techniques can present a serious security risk. Any dynamic content
that is passed to a program from the outside must be checked thoroughly or escaped before being used
in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the predefined function escape
. See
ABAP Command Injections.
Notes
-
Since subroutines are now obsolete as a method of program modularization, a temporary subroutine pool
created using
GENERATE SUBROUTINE POOL
should only contain a single initial subroutine that calls a method of a local class and does not contain any other functional code. - The syntax rules of the ABAP language version of the creator program also apply to the subroutine pool created.
- Using the switch configuration from when the transaction was called for the syntax check ensures that the whole transaction is executed using the same switch configuration (guaranteed by Switch Framework).
-
The source code in the internal table
itab
must contain a complete ABAP program, including the statement that introduces the program. - In a temporary subroutine pool, the same global declarations and editing rules are defined as in the static subroutine pool of the repository (see table of program types).
-
The addition
REDUCED FUNCTIONALITY
of the introductory program statementPROGRAM
also works in temporary subroutine pools and its use is recommended to reduce their resource use. - Temporarily created subroutine pools can be executed in ABAP Debugger in single steps.
- A temporary subroutine pool created for an internal session cannot be deleted explicitly. It remains available from the time it is created up to the point where the internal session is closed.
- The eight-character internal name of a temporary subroutine pool begins with "%_T". This prefix is reserved for temporary subroutine pools.
-
GENERATE SUBROUTINE POOL
should only be used in exceptional cases in application programs. ABAP provides many other means of dynamic programming, which generally make creating source code dynamically unnecessary (see the list in dynamic program processing).
Example
Creates and generates (dynamically) a subroutine pool that implements the event block LOAD-OF-PROGRAM
and two subroutines. Depending on the return code sy-subrc
, a subroutine is called or a message is issued.
DATA tab TYPE STANDARD TABLE OF string WITH EMPTY KEY.
tab = VALUE #(
( `PROGRAM subpool.` )
( `DATA spfli_tab TYPE TABLE OF spfli.` )
( `LOAD-OF-PROGRAM.` )
( ` SELECT *` &
` FROM spfli` &
` INTO TABLE @spfli_tab.` )
( `FORM loop_at_tab.` )
( ` DATA spfli_wa TYPE spfli.` )
( ` LOOP AT spfli_tab INTO spfli_wa.` )
( ` PERFORM evaluate_wa USING spfli_wa.` )
( ` ENDLOOP.` )
( `ENDFORM.` )
( `FORM evaluate_wa USING l_wa TYPE spfli.` )
( ` cl_demo_output=>write_data( l_wa ).` )
( `ENDFORM.` ) ).
GENERATE SUBROUTINE POOL tab NAME DATA(prog)
MESSAGE DATA(mess)
SHORTDUMP-ID DATA(sid).
IF sy-subrc = 0.
PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND.
cl_demo_output=>display( ).
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.
Example
Creates and generates (dynamically) a subroutine pool that implements a local class. The static method meth
of the class can be called using the
absolute type name of the class.
DATA itab TYPE TABLE OF string.
DATA class TYPE string.
itab = VALUE #(
( `program.` )
( `class main definition.` )
( ` public section.` )
( ` class-methods meth.` )
( `endclass.` )
( `class main implementation.` )
( ` method meth.` )
( ` message 'Test' type 'I'.` )
( ` endmethod.` )
( `endclass.` ) ).
GENERATE SUBROUTINE POOL itab NAME DATA(prog).
class = `\PROGRAM=` && prog && `\CLASS=MAIN`.
CALL METHOD (class)=>meth.
Example
Creates and generates (dynamically) a subroutine pool that implements a local class. The class is instantiated using its
absolute type name, and the instance method meth
is called dynamically.
DATA itab TYPE TABLE OF string.
DATA class TYPE string.
DATA oref TYPE REF TO object.
itab = VALUE #(
( `program.` )
( `class main definition.` )
( ` public section.` )
( ` methods meth.` )
( `endclass.` )
( `class main implementation.` )
( ` method meth.` )
( ` message 'Test' type 'I'.` )
( ` endmethod.` )
( `endclass.` ) ).
GENERATE SUBROUTINE POOL itab NAME DATA(prog).
class = `\PROGRAM=` && prog && `\CLASS=MAIN`.
CREATE OBJECT oref TYPE (class).
CALL METHOD oref->('METH').
Executable Example
Exceptions
Handleable Exceptions
CX_SY_GENERATE_SUBPOOL_FULL
-
Cause: No further temporary subroutine pools can be generated.
Runtime error:GENERATE_SUBPOOL_DIR_FULL
CX_SY_GEN_SOURCE_TOO_WIDE
-
Cause: The source code is in a table consisting of strings and the table contains rows with more than 255 characters.
Runtime error:GEN_SOURCE_TOO_WIDE