PPL "How to ..." Guide
Introduction
This guide explains common programming tasks in PPL.
This document is a work in progress. Future versions will include more tips, such as how to work with strings, collections, streams, directories and files, etc.
If you have questions or suggestions then please write to contact {at} ppl-lang {dot} dev.
How to Write and Execute Code
There are two ways to write and execute PPL code:
-
Single program file:
A PPL program file is a single text file that can contain any number of functions and other software components, such as types, factories, services. This is convenient to try out code or to write small applications.
-
Project
A PPL project is composed of hundreds or thousands of source code files hierarchically organized in a directory tree. It is used to write big applications.
The following chapters explain how to use each method.
"Hello World" Program File
To create and run a simple "Hello world" program, proceed as follows:
-
Create the program file
Use your preferred text editor and create a file named hello.ppl in any directory and with the following content:
File hello.ppl
function start write_line ( "Hello World" ) .
NoteA single program file must always contain a function named
start
. As the name suggests, this function is executed when the program is started. -
Execute the program file
Type the following command in a system terminal:
ppl hello.ppl
NoteIf your current working 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/hello.ppl
The result is written to your terminal:
Hello world
"Hello World" Project
-
Create a new project
Open a terminal in any directory in which you want to create the project.
Enter the following commands:
mkdir hello cd hello ppl init
PPL creates directories and files required by an initial project.
-
Write code
An important file is
se_start.ppl
which has been created in the project's subdirectorywork/src/PPL/hello
. This file contains the entry code that will be executed when the application starts.Open file
work/src/PPL/hello/se_start.ppl
with your preferred text editor, and change the source code so that it looks like this:File se_start.ppl
service start function start write_line ( "Hello world" ) . .
Save the file.
-
Compile and build
Execute the
compile_and_build
OS script file which is located in your project’s root directory (i.e.compile_and_build.sh
on Linux/Unix andcompile_and_build.bat
on Windows).Alternatively you can also execute the following two OS commands in a terminal (while being in the project’s root directory):
ppl compile ppl build
-
Execute the application
Execute the
run
OS script file in your project’s root directory (i.e.run.sh
on Linux/Unix systems andrun.bat
on Windows systems).Alternatively, you can also execute the OS command
ppl run
.The application’s output is displayed in the terminal:
Hello world
-
Deploy
For instructions on how to deploy a PPL project please refer to chapter How to Deploy a PPL Application.
How to Try Out Code
The easiest way to try out code (or to write very small applications) is to create a single program file that contains all the source code.
Here is an example of a program that creates a book object and writes its attributes to the OS's output device:
File book_program.ppl
record type book // (1)
attributes
id pos_32
title string
best_seller yes_no default:no
.
.
function create_book -> book // (2)
return book.create (
id = 123
title = "How to Write Beautiful Code"
best_seller = yes )
.
function start // (3)
const book = create_book
write_object_data ( book )
write_new_line
.
(1) Type book
defines a data structure (record) with 3 attributes: id
, title
, and best_seller
(2) Function create_book
creates a book
object and returns it.
(3) As said already in a previous chapter, every program file must contain a start
function which executes first at program startup. In our example, a book
object is created and written to the OS output device.
Executing this program with …
ppl book_program.ppl
... will write the book's attributes to the terminal:
{ "id": 123, "title": "How to Write Beautiful Code", "best_seller": true}
If JavaFX is installed on your system, then you can append the following instructions at the end of function start
:
object_GUI.show ( book )
OS.console?.ask_press_Enter
OS_process.exit_with_success
Now, executing this program with …
ppl book_program.ppl
... will display the book
object in a new window like this:
How to Deploy a PPL Application
When your application is ready to be distributed to other users, you can create a distribution for a Windows, Unix/Linux or macOS machine. A distribution consists of a single compressed file that contains a directory tree. A distribution can only be created for a PPL project - it cannot be created for a single program file.
There are no dependencies. Everything needed to install and run the application is contained in the distribution.
After creating, compiling and building a PPL project, you can create a distribution by entering the following command in the project's root directory:
ppl create_distribution --start my_app
PPL will create a directory named distribution
, located in the project's root directory. This directory contains all the files to be installed on the user's machine.
Creating cross-platform executables with the above command is currently not supported (although it is technically possible). Hence, a distribution created on a Windows machine can only be deployed on a Windows machine. If you need to deploy on a Linux machine, you must create the distribution on a Linux machine. Cross-platform distributions will possibly be added in the future.
The application is launched by executing the my_app
OS script file located in directory distribution/bin
(i.e. file distribution/bin/my_app
on Unix/Linux and distribution/bin/my_app.bat
on Windows). You can change the name of this file with the --start
parameter used in the above command.
You can now compress directory distribution
into a single Windows .zip
or Unix/Linux .tar.gz
file with the tool of your choice.
To install the application on another machine, simply copy the compressed file to the user's machine, and uncompress it.
As said already, the application is launched with an OS script file in directory bin
, for example distribution/bin/my_app.bat
on Windows.
You can add the application's bin
directory to the OS path, so that the application can be started from anywhere without specifying its directory path.