12. Application Startup and Initialization

An application may be written entirely in Perfect, or it may be written partly in Perfect and partly in a traditional programming language. In the latter case, the program entry point may be in the portion of the system written in Perfect, or in the portion written in the programming language. This chapter explains what you need to do in these scenarios to ensure correct application startup and initialization.

12.1 Program entry point written in Perfect

The entry point of a program written in Perfect shall have the following signature:

schema main(args: seq of string,
             context!: limited Environment, ret!: out int)
   pre #args >= 1
   post ... ;

The first element of the args parameter contains the name by which the program was executed, if available, or the empty string if not. The remaining elements of args are the command-line parameters.

The context parameter provides access to the Environment in which the program was executed, so that input/output and similar operations with effects outside the program can be performed. Note that the stdin, stdout and stderr streams are not guaranteed to be open, since on many platforms it is possible to close these streams prior to executing a program.

The ret parameter provides means of returning a return code to the calling program. The program must assign ret before terminating. By convention, a zero return code indicates successful execution.

The postcondition defines what effects the program has. For very simple programs, these affects can be defined directly in the postcondition; for example:

schema main(args: seq of string,
             context!: limited Environment, ret!: out int)
   pre #args >= 1
   post context!print("Hello, world!\n") & ret! = 0;

More usually, you will need to maintain some state in the program. This state is best represented by the abstract variables of some class. If this class is called Application then the main program could look like this:

final class Application ^=
abstract
   var context: Environment,
       ... ;        // declare other state variables here
interface
   build{!context: Environment}
     post ... ;     // initialise other state variables here

   schema !run(args: seq of string, ret!: out int)
     post ... ;
end;

schema main(args: seq of string,
             context!: limited Environment, ret!: out int)
   pre #args >= 1
   post (var myApp: Application! = Application{context};  
         myApp!run(args, ret!)
        );

In this example, we have assumed that the application requires prolonged access to the context, but that it only needs access to the arguments at the start ot a run. Hence we passed the context as a parameter to the constructor, which stored it as part of the state; but we passed the arguments as parameters to the run schema.

12.2 Program entry point not written in Perfect

If the program entry point is not written in Perfect, and the part of the application that is written in Perfect does not perform I/O or use other members of the Environment class, then no special initialization is needed.

If the part of the application that is written in Perfect does need access to an Environment, then it will be necessary to write code in the target programming language to create a single instance of Environment, which can then be passed to the Perfect part of the application. The Environment constructor takes a single argument, which is the path to the directory from which the program was launched (for subsequent use by the getImagePath member).

 

Perfect Language Reference Manual, Version 6.10, December 2013.
© 2012 Escher Technologies Limited. All rights reserved.