Contents

Using Object Icon as a scripting language

Introduction

Although Object Icon has a separate translation and execution process, it is possible (at least on Unix-like systems) to run a program like a script file.

To do this, create a source file in the usual way, but prefix it with the following line, suitably adjusted to reflect the location of your objecticon directory :-

#!/home/rparlett/objecticon/bin/oiscript

For example :-

Download script.icn

#!/home/rparlett/objecticon/bin/oiscript

import io

procedure main(a)
   every write(!a)
end

Next, make the source file executable :-

$ chmod +x script.icn

It can now be run directly :-

$ ./script.icn 1 2 3
1
2
3

Implementation

The implementation of the above is entirely contained in the oiscript file, which is just a simple shell script.

This script tries to minimize the startup time of the program by caching the translated executable files produced by oit. These are stored in a temporary directory (by default /tmp/oixcache), and are named according to the MD5 hash value of the source file. So, for example, after running script.icn, /tmp/oixcache contains :-

$ ls /tmp/oixcache/ -l
total 90,112
-r-xr-xr-x 1 rparlett rparlett 89,082 Aug 18 13:23 13b4874fd6f8f76f8a0cb986a2b93737

When a script file is run, its MD5 hash value is calculated, and if a matching file in /tmp/oixcache is found, that is run directly, without needing to run oit. If it is not found, then oit is run and a new cache file created.

The location of the cache directory can be altered by setting the environment variable OIX_CACHE.

Customization

The oiscript file can be copied to another location and modified as you wish. Then you just have to alter the opening ‘#!’ line in your .icn file to point to the modified script.

&progname

This keyword will be set to the executable file in the cache directory. This may be inconvenient; for example if the ipl.options.options procedure is being used to process command line arguments, then the default usage string will look wrong :-

$ my-prog.icn -help
Usage: 6289838a06433f166f387d4c4f669579 [OPTIONS...]
-t                  Test only
-l                  List output only

One fix for this problem is to simply set &progname to the name of the source file in main(), as follows :-

procedure main(a)
   &progname := &file
   ...
end

Then the output of options would be :-

$ my-prog.icn -help
Usage: my-prog.icn [OPTIONS...]
-t                  Test only
-l                  List output only

Alternatively, options also allows a custom usage string (or procedure) to be passed to it as a parameter.

Contents