Practical use of XECUTE (InterSystems ObjectScript)
If you start with InterSystems ObjectScript, you will meet the XECUTE command.
And beginners may ask: Where and Why may I need to use this ?
The official documentation has a rich collection of code snippets. No practical case.
Just recently, I met a use case that I'd like to share with you.
The scenario:
When you build an IRIS container with Docker, then, in most cases,
you run the initialization script
iris session iris < iris.script
This means you open a terminal session and feed your input line-by-line from the script.
And that's fine and easy if you call methods, or functions, or commands.
But looping over several lines is not possible.
You may argue that running a FOR loop in a line is not a masterpiece.
Right, but the lines are not endless and the code should remain maintainable.
A different goal was to leave no code traces behind after setup.
So iris.script was the location to apply it.
The solution
XECUTE allowed me to cascade my multi-line code.
To avoid conflicts with variable scoping, I just used %Variables
BTW: The goal was to populate some demo LookupTables.
Just for comfort, I used method names from %PopulateUtils as table names
;; generate some demo lookup tables ; inner loop by tableset %1="for j=1:1:5+$random(10) XECUTE %2,%3,%4"; populate with random valuesset %2="set %key=##class(%PopulateUtils).LastName()"set %3="set %val=$ClassMethod(""%PopulateUtils"",%tab)"; write the tableset %4="set ^Ens.LookupTable(%tab,%key)=%val"set %5="set ^Ens.LookupTable(%tab)=$lb($h) write !,""LookupTable "",%tab"; main loopXECUTE"for %tab=""FirstName"",""City"",""Company"",""Street"",""SSN"" XECUTE %1,%5";; just in DockerThe result satisfied the requirements without leaving permanent traces behind
And it did not interfere with the code deposited in IPM.
So it was only used once by Docker container build.
Comments
Back to where I started.
I like this still works