Syntax
At the core of Lasso’s coding ease is its syntax. Simple, logical and straightforward, it’s similar enough to other languages to be easily mastered, but a world apart once you’re in. A brief overview is presented here. For complete details, see the Language Guide.
Embedding and Output
Lasso is most often used to generate HTML pages, and thus is commonly seen embedded in HTML. Standard processing delimiters ('<?lasso' and '?>') are used to indicate blocks of Lasso code within an HTML page. Two shortened forms are also supported for brevity. The following example shows all three forms in use:
<html>
<?lasso
local(name = action_param('name'),
day = date->format('%A'))
?>
<body>
<h1>Hello, <?= #name ?></h1>
<p>Today is [#day].</p>
</body>
</html>
As of Lasso 9, Lasso code can also be used to create shell scripts with a hashbang (#!) followed by the path to Lasso on your system:
#!/usr/local/bin/lasso9
'The current date is ' + date
Note that Lasso is output-oriented, meaning that there is no need for a separate "print", "echo", or "output" function to add program output to the page buffer (or stdout, as the case may be).
Variables
Lasso supports two kinds of variables: thread variables and local variables. Thread variables are specific to the current thread (commonly a single web request), while local variables are confined to the method, or function, in which they are created. Lasso variables must be defined before they can be accessed.
// define a local variable, foo, and access it
local(foo = 'bar')
#foo
// define a thread variable, foo, and access it
var(foo = 'bar')
$foo
Conditionals & Loops
Lasso provides various constructs for handling conditional logic. These constructs can be written as an association or a container. Associations use a givenBlock to enclose statements, and resemble the syntax used by other scripting languages. Containers instead use a closing sequence, and operate more like closing tags in HTML.
// association
if(expression) => {
// do something...
else
// do something else...
}
// container
if(expression)
// do something...
else
// do something else...
/if
Other types of conditionals include match, while, loop, and iterate:
match(expression) => {
case(case1, case2)
// execute for case 1 or 2...
case(case3)
// execute for case 3...
case
// default...
}
while(expression) => {
// do as long as expression is true...
}
loop(5) => {
// execute 5 times...
}
local(foo = array('A','B','C'))
iterate(#foo) => {
'Position in #foo: ' + loop_count + '<br/>'
'Value at that position: ' + loop_value
}
Methods (Functions)
Lasso's function abstraction is called a method, and makes use of the define keyword to create reusable functionality:
define greeting(first_name, last_name) => {
return 'Hello ' + #first_name + ' ' + #last_name + '.'
}
Error Handling
Trapping for errors is accomplished via protect/handle blocks, similar to the try/catch/finally blocks used in other languages:
protect => {
handle_error => {
// execute if an error occurs...
}
handle(condition) => {
// execute if condition is true...
}
handle => {
// execute always, as the current block completes...
}
// some code that causes an error...
}
Query Expressions
Query Expressions use a single, natural-language syntax to sort, filter, and merge data from multiple independent sources (databases, text files, web services, and more) simultaneously. Query Expressions are a 'killer feature' of Lasso 9. Once learned you'll wonder how you ever coded without using it.
// square each number in the array
with num in array(1, 2, 3, 4, 5, 6, 7, 8, 9)
select #num * #num
// average of the numbers in the array
with num in array(1, 2, 3, 4, 5, 6, 7, 8, 9)
average #num
// select all non-vowel alphabetic characters in the sentence
with word in 'Violence is the last refuge of the incompetent.'->eachWord
with char in #word->eachCharacter
where #char->isAlpha
where !(:'a','e','i','o','u')->contains(#char)
select #char