AtlatestRepositorysigil-web-styles
sigil-web-styles / tree / build / dev / lib / _pkg / sigil-stdlib / languagemodules.md
2
# Modules4
> Library definition, imports, and exports.6
## File Extension8
Sigil source files use the `.sgl` extension:9
- `main.sgl` - source files10
- `utils.sgl` - library modules11
- `package.sgl` - package definitions13
The `.scm` and `.sld` extensions are supported for compatibility with existing Scheme code, but `.sgl` is the default and preferred extension for new Sigil projects.15
## define-library17
Define a module with explicit imports and exports.19
```scheme20
(define-library (myapp utils)21
(import (sigil string))22
(export helper-function23
useful-constant)24
(begin25
(define useful-constant 42)27
(define (helper-function x)28
(string-append "Result: " (number->string x)))))29
```31
### Structure33
A library definition contains:34
- **Name**: List of symbols, e.g., `(myapp utils)`35
- **Imports**: Libraries to import36
- **Exports**: Bindings to make public37
- **Body**: Definitions (inside `begin`)39
### File Naming41
Library name maps to file path:42
- `(sigil json)` → `sigil/json.sgl`43
- `(myapp utils)` → `myapp/utils.sgl`45
Files are searched in the library load path (`SIGIL_LIB_PATH`).47
## import49
Bring bindings from other libraries into scope.51
```scheme52
;; Import entire library53
(import (sigil json))55
;; Import multiple libraries56
(import (sigil string)57
(sigil path))58
```60
Note: `(sigil core)` is automatically imported into every module—you don't need to import it explicitly.62
### Selective Import64
```scheme65
;; Import only specific bindings66
(import (only (sigil string)67
string-split68
string-join))70
;; Import all except certain bindings71
(import (except (sigil io) display))73
;; Rename on import74
(import (rename (sigil json)75
(json-encode encode)76
(json-decode decode)))78
;; Add prefix to all imports79
(import (prefix (sigil http) http:))80
; Now use http:get, http:post, etc.81
```83
### Combining Forms85
```scheme86
(import (only (rename (sigil json)87
(json-encode encode))88
encode))89
; Imports json-encode as encode90
```92
## export94
Declare which bindings are public.96
```scheme97
(define-library (myapp api)98
(export public-function99
PublicRecord100
CONSTANT)101
(begin102
;; Exported103
(define (public-function x) ...)104
(define-record-type PublicRecord ...)105
(define CONSTANT 100)107
;; Not exported (private)108
(define (internal-helper x) ...)))109
```111
### Rename on Export113
```scheme114
(export (rename internal-name external-name))115
```117
## Implicit Imports119
Every module implicitly imports `(sigil core)`, which provides:120
- Output: `println`, `eprintln`, `print`, `eprint`, `format`121
- Basic list operations: `map`, `filter`, `fold-left`, `append`122
- Predicates: `null?`, `pair?`, `number?`, `string?`123
- Arithmetic: `+`, `-`, `*`, `/`, `=`, `<`, `>`124
- Control: `if`, `cond`, `case`, `when`, `unless`125
- And more...127
You don't need to explicitly import `(sigil core)`.129
## Library Search Order131
When importing a library, Sigil searches:133
1. **Load path directories** (`SIGIL_LIB_PATH`, in order)134
2. **Embedded modules** (in bundled executables)135
3. **Standard library locations**137
First match wins. This allows overriding standard libraries for debugging.139
## Example: Complete Module141
```scheme142
;;; (myapp strings) - String utilities for the myapp project.144
(define-library (myapp strings)145
(import (sigil string))146
(export slug147
title-case)148
(begin150
;;; Convert a string to a URL-friendly slug.151
(define (slug str)152
(string-join153
(string-split (string-downcase str) " ")154
"-"))156
;;; Capitalize the first letter of each word.157
(define (title-case str)158
(string-join159
(map capitalize-word160
(string-split str " "))161
" "))163
;; Internal helper164
(define (capitalize-word word)165
(if (string-empty? word)166
word167
(string-append168
(string-upcase (substring word 0 1))169
(substring word 1))))))170
```