Lesson 11 of 16

Standard Library

files, OS, JSON, regex, dates, math, random

download this lesson (.md) ↓ all lessons

11. Standard Library

A tour of the built-in functions. (Networking, graphics, and database have their own lessons: 12, 13, 14.)

Files

write_file("data.txt", "line 1\n");      // create/overwrite, returns bool
append_file("data.txt", "line 2\n");     // append
print(file_exists("data.txt"));          // true
print(read_file("data.txt"));            // whole file as a string (null if missing)
print(list_dir("."));                    // list of names in a folder
make_dir("output");                      // create folder(s)
delete_file("data.txt");                 // remove a file

OS / system

print(args());        // command-line args after the script: stencil app.st X Y -> [X, Y]
print(env("PATH"));   // an environment variable (null if unset)
print(cwd());         // current working directory
let out = exec("echo hello");   // run a shell command, return its stdout
print(out);
// exit(0);           // stop the program with an exit code

JSON

let data = { "name": "Ember", "langs": ["STencil", "Python"], "age": 20 };
let text = json_str(data);            // compact JSON string
print(text);                          // {"age":20,"langs":["STencil","Python"],"name":"Ember"}
print(json_pretty(data));             // indented JSON
let back = json_parse(text);          // parse JSON -> dict/list/...
print(back["name"], back["langs"][0]);  // Ember STencil

Regular expressions

print(regex_match("[0-9]+", "abc123"));          // true
print(regex_find("[0-9]+", "abc123def456"));     // 123
print(regex_find_all("[0-9]+", "a1b22c333"));    // [1, 22, 333]
print(regex_replace("[aeiou]", "merhaba", "*")); // m*rh*b*

Date & time

print(now());                       // milliseconds since 1970 (int)
print(timestamp());                 // seconds since 1970 (int)
print(date());                      // "2026-06-30 11:29:53" (local)
print(date_format("%d/%m/%Y"));     // custom format

Math

print(abs(-7), round(3.6), floor(3.9), ceil(3.1));  // 7 4 3 4
print(sqrt(144.0), pow(2, 10));                     // 12.0 1024
print(sin(0.0), cos(0.0), log(2.718281828));        // 0.0 1.0 ~1
print(min([5,2,8]), max([5,2,8]), sum([1,2,3]));    // 2 8 6

Random (for games & simulations)

print(random());                       // float 0..1
print(randint(1, 6));                  // dice roll, inclusive
print(choice(["rock","paper","scissors"]));
print(shuffle([1,2,3,4,5]));           // a shuffled copy

Input

let name = input("Your name: ");   // read a line from the user
print("Hi " + name);

Next: Networking & Web