Data declarations in procedures create local data types and objects that are only visible within that procedure. There are two kinds of data types and objects - dynamic and static. Dynamic data objects only exist while the subroutine is running, while static objects still exist after the subroutine has finished running, and retain their values until the next time the subroutine is called. Field symbols can be declared locally. You can also use a special kind of data object for subroutines - copies of global data on a local data stack. You define and address them using field symbols.
Local data types and objects declared in subroutines using the TYPES and DATAstatements are deleted when the subroutine ends, and recreated each time the routine is called.
Every subroutine has its own local namespace. If you declare a local data type or object with the same name as a global data type or object, the global type or object cannot be addressed from within the subroutine. Local data types or data objects hide identically named global data types or objects. This means that if you use the name of a data type or object in the subroutine, you always address a locally declared object - if this exists - and otherwise a globally declared one. To prevent global data types or objects from being hidden, local types and objects must have other names assigned to them. For example, all local names in subroutines could begin with 'F _'.
REPORT demo_mod_tech_data_types .
TYPES word(10) TYPE c.DATA text TYPE word.
text = '1234567890'. WRITE / text.
PERFORM datatest.
WRITE / text.
FORM datatest. TYPES word(5) TYPE c. DATA text TYPE word. text = 'ABCDEFGHJK'.WRITE / text.ENDFORM.
When you run the program, the following is displayed:
1234567890
ABCDE
1234567890
In this example, a data type word and a global data object text with type word are declared globally in the main program. After a value has been assigned to text and this has been displayed on the list, the internal subroutine datatest is called. Inside the subroutine, a data type word and a local data object text with type word are declared locally. They hide the global type and object. Only after the subroutine is finished are the global definitions are valid again.
If you want to keep the value of a local data object after exiting the subroutine, you must use the STATICS statement to declare it instead of the DATA statement. With STATICS you declare a data object that is globally defined, but only locally visible from the subroutine in which it is defined.
REPORT demo_mod_tech_statics.
PERFORM datatest1.PERFORM datatest1.
SKIP.
PERFORM datatest2.PERFORM datatest2.
FORM datatest1. TYPES f_word(5) TYPE c. DATA f_text TYPE f_word VALUE 'INIT'. WRITE f_text. f_text = '12345'. WRITE f_text.ENDFORM.
FORM datatest2. TYPES f_word(5) TYPE c. STATICS f_text TYPE f_word VALUE 'INIT'. WRITE f_text. f_text = 'ABCDE'. WRITE f_text.ENDFORM.
When you run the program, the following is displayed:
INIT 12345 INIT 12345
INIT ABCDE ABCDE ABCDE
In this example, two similar subroutines datatest1 and datatest2 are defined. In datatest2 , the STATICSstatement is used instead of the DATA statement to declare the data object f_text. During each call of datatest1, f_text is initialized again, but it keeps its value for datatest2. The VALUE addition of the STATICSstatement functions only during the first call of datatest2.
All field symbols declared in a subroutine using the FIELD-SYMBOLS statement are local. For local field symbols, the following rules apply:
In a subroutine, you can create local copies of global data on the local stack. To do this, use a local field symbol and the following variant of the ASSIGNstatement:
ASSIGN LOCAL COPY OF field TO <fs>.
The system places a copy of the specified global field field on the stack. In the subroutine, you can access and change this copy without changing the global data by addressing the field symbol <fs>.
You can use the LOCAL COPY OFaddition with all variants of the ASSIGN statement, except ASSIGN COMPONENT.
Other variants of the ASSIGN statement that are used in subroutines are:
ASSIGN LOCAL COPY OF INITIAL field TO <fs>.
This statement creates an initialized copy of the global field field on the stack without transporting the field contents.
ASSIGN LOCAL COPY OF INITIAL LINE OF itab TO <fs>.
This statement creates an initial copy of the lines of a global internal table itab on the stack.
ASSIGN LOCAL COPY OF INITIAL LINE OF (field) TO <fs>.
This statement creates an initial copy of the lines of a global internal table itab on the stack. The internal table is specified dynamically as the contents of the field field.
REPORT demo_mod_tech_assign_local_cop.
DATA text(5) TYPE c VALUE 'Text1'.
PERFORM routine.
WRITE text.
FORM routine. FIELD-SYMBOLS <fs> TYPE ANY. ASSIGN LOCAL COPY OF text TO <fs>. WRITE <fs>. <fs> = 'Text2'. WRITE <fs>. ASSIGN text TO <fs>. WRITE <fs>. <fs> = 'Text3'.ENDFORM.
The list output is:
Text1 Text2 Text1 Text3
By assigning the field text to the local subroutine <fs> in the subroutine routine, you place a copy of text on the local data stack. By addressing <fs>, you can read and change this copy. The global field text is not affected by operations on the local copy. After you have assigned the field to the field symbol without the LOCAL COPY OF addition, the field symbol points directly to the global field. Operations with the field symbol then affect the global field.