Importing other Aptilis scripts
|
|
Advanced topics
|
|
1. Introduction
2. How to use imports
3. Import conventions
4. Examples
1. Introduction
Imports allow you to re-use code and to keep your scripts concise.
The binary distributions of Aptilis do already include a small library of scripts, which you can use in your programs.
Aptilis has adopted the main characteristics of the package model from Java to avoid collisions between the scripts of different people. See Import conventions.
2. How to use imports
- Imports must be situated at the beginning of a program, before sub definitions.
- the syntax of the import statement is:
import dir.subdir.subsubdir.aptilisfile [as synonym]
Specifying a synonym is optional.
- Aptilis tries to follow the java conventions for packets:
Directories must be separated by '.' not:
/ or \
- If the file is CurrencyUtils.e.txt then aptilisfile above must be:
CurrencyUtils not CurrencyUtils.e.txt
- If aptilisfile is MyUtils, Aptilis will look for the following files, in this order:
MyUtils.e.txt
MyUtils.apt
MyUtils.aptilis
- Aptilis will look for the file to import from the following directories in this order:
- Directory pointed to by the environment variable APTILISPATH
- Directory where the script is.
- Directory pointed to by the environment variable CLASSPATH (the Java one).
- The current directory - this may be where the interpreter (aptilis.exe) is but not necessarily. Use getCurrentDirectory() to find out where you are!
- Folders containing Aptilis files to be imported, and these Aptilis files may ONLY contain the following charater ranges in their names: a-z, A-Z, 0-9
In other words the following characters are explicitely forbidden: _ - . / \
- File names are case sensitive under Unix, but not under windows. However in any case, RESPECT case.
- Synonyms follow variable name conventions.
They may only contain letters, numbers, underscores, and have to start with a letter.
- Paths in the APTILISPATH follow the same rules as for java's CLASSPATH.
- Paths are separated by ; under windows : under unix.
- Directories in paths are separated by \ under windows and / under unix, etc.
- Directories (that are likely to be files) whose names end in .zip or .jar or .aar (for possible future aptilis archives) are ignored.
- Directories are case sensitive under Unix but NOT under Windows.
3. Import conventions:
Conventions are necessary to ensure that your own "library" doesn't come into conflict with the one of someone else.
- Packages should all have their directory names in lower case
- Filenames and synonyms should start with a capital
- Subs should start with a capital (auxiliary subs may be in lower case)
- Initialisations (global vars) should be done in an "Init" sub.
- Aptilis standard packages are available from apt.
- Any one else's packages should be their domain names reversed.
CNN would be:
com.cnn (...)
SourceForge would be:
net.sourceforge (...)
You will need a directory structure like this: "./com/myserver/mypackages"
4. Examples
These examples use the standard Aptilis library.
Example 1:
import apt.template.Html as Html
sub main()
doctype = Html.Get("doctype_4_01_strict") $
title = "Welcome to my Homepage" $
body = "<h1>Hi there!</h1>" $
head = "<meta name=\"author\" content=\"John Doe\">" $
print("Content type: text/html\n\n")
print(stuff(Html.Get("skeleton")$)$)
end main
Result:
Content type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Welcome to my Homepage</title>
<meta name="author" content="John Doe">
</head>
<body >
<h1>Hi there!</h1>
</body>
</html> |
Example 2:
import apt.http.Cookie
sub main()
cookiedata["ip"] = _ENV["REMOTE_ADDR"] $
cookiedata["browser"] = _ENV["HTTP_USER_AGENT"] $
apt.http.Cookie.Set(cookiedata[],"")
print("Content-type: text/plain\n\n")
oldcookie[] = apt.http.Cookie.Get()
if cookiedata["browser"] != oldcookie["browser"] $
print("Hey! You changed your browser!")
end if
end main