Chapter 1. Working With PPL Script Files

Table of Contents
Introduction
Simple Example
OS Command Line Arguments
Embedding Java Source Code
First Class Executable

Introduction

You can put PPL source code instructions into a single text file and then execute them immediately. This is called scripting in PPL.

Scripting is typically used for testing out source code snippets or for writing small command line utilities.

The following features are supported:

  • any valid PPL instruction can be executed in a script file

  • OS command line arguments can be accessed

  • script files can contain embedded Java source code

  • first class executables can be created in Unix/Linux systems

Simple Example

To create and execute a simple script file proceed as follows:

  • Use your preferred text editor and create a file named test.ppl in any directory and with the following content:

    repeat 3 times
       OS.out.write_line ( "Hello world" )
    .
    
    GUI_dialogs.show_info_and_wait (
       title = "Hello"
       info = "Hello world" )
    
  • Execute the script by typing the following command in a system terminal:

    ppl test.ppl
    [Note]Note
    If your current directory is different from the directory of the script file then you must specify a relative or absolute path to the script file, for example:
    ppl ../tests/test.ppl
  • The above script first writes to the OS out device ...

    Hello world
    Hello world
    Hello world

    ... then opens a GUI dialog and waits for a user confirmation:

    Hello world GUI in PPL

OS Command Line Arguments

Within a script you can access input arguments provided on the OS command line. The command line arguments are available as a list of nullable strings in input argument command_line_arguments which is implicitly defined. The type of this input argument is nullable indexed_list<nullable string>. The following script shows how to read the values:

if i_command_line_arguments is not null then

   OS.out.write_line ( """Number of command line arguments: {{i_command_line_arguments.size}}""" )

   // classic loop and if
   repeat for each argument in i_command_line_arguments
      if argument is not null then
         OS.out.write_line ( argument )
      else
         OS.out.write_line ( "null" )
      .
   .

   // functional loop using a closure and 'if_null:' operator
   stream<nullable string>.create_from_collection ( i_command_line_arguments ).for_each ( {
      OS.out.write_line ( object if_null: "null" )
   } )

else
   OS.out.write_line ( "There are no command line arguments." )
.

Save the above source code in file script_arguments_test.ppl and then execute it by typing:

ppl script_arguments_test.ppl Hello "" "Lisa Simpson"

The following output will be displayed:

Number of command line arguments: 4
script_arguments_test.ppl
Hello
null
Lisa Simpson
script_arguments_test.ppl
Hello
null
Lisa Simpson

As you can see, the name of the script file is the first argument.

Embedding Java Source Code

Besides PPL source code, a script can also contain Java source code.

To embed Java source code in a script, simply insert the Java statements between a java line and an end java line, as in the following example:

java
   System.out.println ( "Hello world" );
end java

You can also mix PPL and Java code in a script like this:

// loop with PPL; display with Java
repeat 3 times
   java
      System.out.println ( "Hello world from Java" );
   end java
.

// loop with Java; display with PPL
java
   for ( int i = 1; i <= 2; i++ ) {
end java
      OS.out.write_line ( "Hello world from PPL" )
java
   }
end java

Executing the above script will display:

Hello world from Java
Hello world from Java
Hello world from Java
Hello world from PPL
Hello world from PPL

First Class Executable

If you use Unix/Linux, you can turn a PPL script file into a first class executable, by using the so-called shebang syntax.

To do this, insert a first line as shown below in your script file:

#!/usr/bin/env ppl

OS.out.write_line ( "Hello world" )

Make your file executable. If your file is named hello.ppl then the command would be:

chmod a+x hello.ppl

Now you can execute hello.ppl by simply typing:

./hello.ppl

Result displayed in your terminal:

Hello world