Globals in 4gl - feature or bug?
Posted in 1996
Quoted from the 4gl reference manual, volume 2: 1. You may have at most one GLOBALS statement where global variables are defined. This statement may be in a separate file or in the file containing the main program block. For years I'va had this strange belief that in a multiple file application you could only have one place where globals were defined, all other files having to refer to that. On friday, with no apparent reason other than the desire to acquire more knowledge, I tried the following: ---- main.4gl ---- main call a() call b() call c() call d() call e() end main ---- a.4gl ---- globals "ag.4gl" function a() let a1=1 display a1 at 1, 1 end function ---- b.4gl ---- globals "ag.4gl" function b() let a1=a1+1 display a1 at 2, 2 end function ---- c.4gl ---- globals "cg.4gl" function c() display "3" at 3, 3 end function ---- d.4gl ---- function d() display "4" at 4, 4 end function ---- e.4gl ---- globals define e1 smallint end globals function e() display "5" at 5, 5 end function ---- ag.4gl ---- globals define a1 smallint end globals ---- cg.4gl ---- globals define c1 smallint end globals it compiled with no complaints (c4gl 4.10UC1) and worked as expected! Hmmm.... surely the one globals limitation must have something to do with the p-code runner. I dug up the long lost i4gl (1.10.03) to give the code a try, and hear, hear, it compiled & worked too! It turns out that in every module you can have a global statement in which you can declare only the globals you need (with globals playing the role of "extern" as well), or reference a file that does. You can have multiple globals files, each defining the exact subset of globals a bunch of modules needs. Just to add to Kerry's rantings on reusable code, and more specifically to modular variables, this makes it possible to split a large module (to contain module size, to gain in readability or to utilize part of it in other contexts) and have multi-modular variables. Fglgo does even the job of catching globals defined in different ways by different modules (ld doesn't!). This redefines my concept of global variables. They appear to be less dangerous than I thought. I don't want to start a thread on the evil and good of global variables, I only want to know: 1) Is this a feature? - I suppose it is. 2) if it is, is it generally known? (in other words, am I the only ignorant?) 3) The reason of the single globals limitation. As a small aside, I would like to add to the "dates before Christ" thread by noting that many times the FM is not at all clear, and this seems to be one of those. cheers, marco ____________________________________________________________________________ rem radioterapia, which I immeritately manage, seldom agrees with what I say marco greco (Catania, Italy) Work: marcog@ctonline.it rem radioterapia 39 95 447828 fax 446558 (was mar.greco@agora.stm.it) Achea 39 95 503117