gnucobol-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[open-cobol-list] Nested sub program problem illustrated


From: Gary Cowell
Subject: [open-cobol-list] Nested sub program problem illustrated
Date: Wed, 4 Feb 2009 09:34:14 +0000 (GMT)

There is a problem with nested sub programs, they go wrong when named the same 
and it's causing major problems.

Here are two small example programs:

        identification division.
        program-id. prog1.
        data division.
        working-storage section.
        01      myitems global.
                03 myitem1 pic x(10).
                03 myitem2 pic 9.
        procedure division.
        a-main1 section.
                move 'hello' to myitem1
                move 8 to myitem2
                display 'a-main1 prog1'
                display 'a-main1 myitem1 ' myitem1
                display 'a-main1 myitem2 ' myitem2
                call 'nestedprog'
                call 'prog2'
                call 'nestedprog'
                display 'bye'
                goback.

        identification division.
        program-id. nestedprog.
        procedure division.
        b-main1 section.
                display 'nestedprog in prog1'
                display 'nestedprog in prog1 myitem1 '
                        myitem1
                display 'nestedprog in prog1 myitem2 '
                        myitem2
                goback.
        end program nestedprog.
        end program prog1.


and the other one

        identification division.
        program-id. prog2.
        data division.
        working-storage section.
        01      myitems global.
                03 myitem1 pic x(10).
                03 myitem2 pic 9.
        procedure division.
        a-main1 section.
                move 'goodbye' to myitem1
                move 4 to myitem2
                display 'a-main1 prog2'
                display 'a-main1 myitem1 ' myitem1
                display 'a-main1 myitem2 ' myitem2
                call 'nestedprog'
                display 'goback to prog1'
                goback.

        identification division.
        program-id. nestedprog.
        procedure division.
        b-main1 section.
                display 'nestedprog in prog2'
                display 'nestedprog in prog2 myitem1 '
                        myitem1
                display 'nestedprog in prog2 myitem2 '
                        myitem2
                goback.
        end program nestedprog.
        end program prog2.



You can see that both programs contain a nested sub program named 'nestedprog' 

The problem is, when prog2 tries to call it's copy of 'nestedprog' open cobol 
runs the 'nestedprog' from prog1 instead. Worse, it uses the storage from prog1.

Resulting in the following output:

$ cobc prog1.cbl
$ cobc prog2.cbl
$ cobcrun prog1
a-main1 prog1
a-main1 myitem1 hello
a-main1 myitem2 8
nestedprog in prog1
nestedprog in prog1 myitem1 hello
nestedprog in prog1 myitem2 8
a-main1 prog2
a-main1 myitem1 goodbye
a-main1 myitem2 4
nestedprog in prog1
nestedprog in prog1 myitem1 hello
nestedprog in prog1 myitem2 8
goback to prog1
nestedprog in prog1
nestedprog in prog1 myitem1 hello
nestedprog in prog1 myitem2 8
bye

So when you see "a-main1 prog2" we have moved goodbye and 4 to our storage, but 
when we call 'nestedprog' it goes off to prog1 and uses 'hello' and 8 again. 
This is not correct.

Micro Focus Server Express runs the programs like this:

$ cobrun ./prog1
a-main1 prog1
a-main1 myitem1 hello
a-main1 myitem2 8
nestedprog in prog1
nestedprog in prog1 myitem1 hello
nestedprog in prog1 myitem2 8
a-main1 prog2
a-main1 myitem1 goodbye
a-main1 myitem2 4
nestedprog in prog2
nestedprog in prog2 myitem1 goodbye
nestedprog in prog2 myitem2 4
goback to prog1
nestedprog in prog1
nestedprog in prog1 myitem1 hello
nestedprog in prog1 myitem2 8
bye





reply via email to

[Prev in Thread] Current Thread [Next in Thread]