references/sleep-functions/QUICKREF.md
GitHubSleep Quick Reference Guide
A fast reference for the most commonly used Sleep functions and concepts.
Essential Functions
Output
println($text)- Print text with newlineprint($text)- Print text without newlinewarn($message)- Print warning message
Variables
local('$var')- Declare local variableglobal('$var')- Declare global variable$null- Null/empty scalar
Arrays
@array = @(1, 2, 3)- Create arraypush(@array, $item)- Add to endpop(@array)- Remove from endsize(@array)- Get sizeforeach $item (@array) { }- Iterate
Hashes
%hash = %(key => "value")- Create hash%hash["key"]- Access valuekeys(%hash)- Get all keysvalues(%hash)- Get all values
Strings
strlen($str)- Get lengthsubstr($str, $start, $len)- Get substringsplit($delim, $str)- Split into arrayjoin($delim, @array)- Join arrayuc($str)- Uppercaselc($str)- Lowercase
Control Flow
if ($condition) { }- Conditionalwhile ($condition) { }- Loopfor ($i = 0; $i < 10; $i++) { }- For loopforeach $item (@array) { }- For each
Functions
sub name { }- Define functionreturn $value- Return from functionlambda({ code }, $var => $val)- Anonymous function
File I/O
openf($filename)- Open file for readingopenf(">$filename")- Open for writingreadln($handle)- Read lineprintln($handle, $text)- Write lineclosef($handle)- Close file
Regular Expressions
"string" ismatch 'pattern'- Test match"string" hasmatch 'pattern'- Find matchmatched()- Get match resultsreplace($str, 'pattern', 'replacement')- Replace
Math
int($value)- Convert to integerdouble($value)- Convert to doublerand()- Random numbersqrt($n),sin($n),cos($n)- Math functions
Objects (Java Integration)
[new ClassName]- Create object[object method: arg1, arg2]- Call method[ClassName staticMethod: arg]- Static methodimport package.*- Import Java package
Common Patterns
Reading a File
$handle = openf("file.txt");
while $line (readln($handle)) {
println($line);
}
closef($handle);
Writing a File
$handle = openf(">output.txt");
println($handle, "Line 1");
println($handle, "Line 2");
closef($handle);
Iterating Arrays
@items = @("a", "b", "c");
foreach $item (@items) {
println($item);
}
Iterating Hashes
%data = %(key1 => "val1", key2 => "val2");
foreach $key => $value (%data) {
println("$key = $value");
}
Try-Catch
try {
# risky code
} catch $error {
warn("Error: $error");
}
Creating Threads
$thread = fork({
println("In thread!");
}, $var => $value);
wait($thread);
Operators
Comparison
==- Numeric equalityeq- String equality<,>,<=,>=- Numeric comparisonlt,gt- String comparison
Logical
&&- AND||- OR!- NOT
Assignment
=- Assign+=,-=,*=,/=- Modify and assign
String
.- Concatenatex- Repeat
Predicates
Type Testing
-isnumber $x- Is number-isarray $x- Is array-ishash $x- Is hash-isfunction $x- Is function
File Testing
-exists $path- File exists-isDir $path- Is directory-isFile $path- Is file-canread $path- Can read-canwrite $path- Can write
Stream Testing
-eof $handle- End of file
Special Variables
@_- Anonymous arguments in function$1, $2, $3- Positional arguments$0- Message name in closure$null- Null value$this- Current closure@ARGV- Command line arguments$__SCRIPT__- Current script name
Common Idioms
Default Value
$value = iff($x is $null, "default", $x);
Safe Array Access
$item = iff(size(@array) > 0, @array[0], $null);
String Formatting
println("Value is $value");
println("Format: $[10]value"); # Right-align in 10 chars
Array Slicing
@slice = sublist(@array, $start, $end);
Quick Tips
- Variables start with symbols:
$scalar,@array,%hash,&function - Arrays are 0-indexed: First element is
@array[0] - Negative indices work:
@array[-1]is last element - Functions use &: Reference with
&functionName - Pass by reference: Arrays, hashes, objects pass by reference
- Closures have scope: Use
lambda()to create isolated scope - Regex anchored:
ismatchanchors to start/end,hasmatchdoesn't - Whitespace matters: Operators need whitespace:
$x = 1 + 2
Getting Help
- Full Documentation: See INDEX.md for complete function reference
- Tutorials: See intro.md, functions.md, regex.md, etc.
- Examples: Most function files include usage examples
- Online: http://sleep.dashnine.org/
Most Used Functions (Top 20)
println- Output textopenf- Open filereadln- Read lineclosef- Close handlesize- Get array/hash sizeforeach- Iterate collectionpush- Add to arraypop- Remove from arraysplit- Split stringjoin- Join arraysubstr- Get substringismatch- Regex matchif/else- Conditionalwhile- Looplocal- Declare variablereturn- Return valuekeys- Hash keysiff- Conditional operatorlambda- Anonymous functioneval- Evaluate code
For complete documentation, see INDEX.md