Organize your code into modules for better reusability and maintainability.
Modules are separate files containing Pome code that you can import into other files. They allow you to:
- Organize code into logical units
- Reuse code across multiple projects
- Hide implementation details
- Manage dependencies
Modules are .pome files:
project/
├── main.pome
├── math_utils.pome
├── string_utils.pome
└── data/
└── config.pome
Use the export keyword to make code available to other modules:
// math_utils.pome
export fun add(a, b) {
return a + b;
}
export var pi = 3.14159;
You can export multiple items at once using an export block:
var a = 1, b = 2;
fun sum(x, y) { return x + y; }
export { a, b, sum };
The import statement loads an entire module:
import math_utils;
print(math_utils.add(5, 3));
Use from ... import to pull specific symbols into your local scope:
from math_utils import add, pi;
print(add(5, 3));
print(pi);
When you import a module, Pome looks for it in the following order:
- Script Directory: The directory containing the script being executed.
- Current Directory: The current working directory.
- Environment Variables: Directories listed in
POME_PATH. - Built-in Modules: Modules like
math,io,string,time.
You can import modules in subdirectories using path separators:
import utils/string_helper;
// Access symbols via the base name:
string_helper.trim(" text ");
Pome automatically uses the last part of the path as the module's name in your local scope.
Pome includes several built-in modules:
import math;
print(math.pi);
print(math.sin(math.pi / 2));
print(math.cos(0));
print(math.random());
import string;
var text = "Hello World";
var substring = string.sub(text, 0, 5);
print(substring); // Output: Hello
import io;
io.writeFile("output.txt", "Hello from Pome");
var content = io.readFile("output.txt");
print(content);
-
One responsibility per module: Keep related functions together
// Good structure math_utils.pome // Math operations string_utils.pome // String operations models.pome // Data models -
Use clear names: Module names should describe their content
user_validator.pome // Validates users database_connection.pome // Database operations -
Hide implementation details: Only export necessary items
// string_utils.pome fun _private_helper(str) { // Internal implementation return str; } export fun publicFunction(str) { return _private_helper(str); } -
Document exported interfaces: Make clear what modules provide
// config.pome // Configuration module provides application settings // Exports: DB_HOST, DB_PORT, LOG_LEVEL, getConfig() export var DB_HOST = "localhost"; export var DB_PORT = 5432; -
Avoid circular dependencies: Module A importing Module B that imports A
// Avoid this structure // models/user.pome imports services/user_service.pome // services/user_service.pome imports models/user.pome -
Group related modules in directories:
project/ ├── models/ │ ├── user.pome │ ├── book.pome │ └── comment.pome ├── services/ │ ├── user_service.pome │ ├── book_service.pome │ └── ... └── utils/ ├── math_utils.pome └── string_utils.pome
my_app/
├── main.pome
├── config.pome
├── models/
│ ├── user.pome
│ ├── product.pome
│ └── order.pome
├── services/
│ ├── user_service.pome
│ ├── product_service.pome
│ └── order_service.pome
├── utils/
│ ├── validation.pome
│ ├── formatting.pome
│ └── array_utils.pome
└── data/
└── database.pome
// main.pome
import config;
import models/user;
import models/product;
import services/user_service;
import utils/validation;
// Application code
Next: Standard Library
Back to: Documentation Home