Qore Programming Language Reference Manual
1.12.0
|
Non-block statements in Qore are always terminated by a semi-colon ";"
as in Perl, C, C++, or Java. Statements can be grouped into blocks, which are delimited by curly brackets "{"
and "}"
containing zero or more semi-colon delimited statements, as in C or Java. Like C, C++, and Java, but unlike perl, any Qore statement taking a statement modifier will accept a single statement or a statement block.
A statement can be any of the following (note that statements are also recursively defined, and note furthermore that all examples are given in %new-style):
Qore Statements
Type | Examples | Reference |
An expression that changes an lvalue | var = 1;
var += 5;
var[1].count++;
shift var.key[i];
| Expressions |
An expression with the new operator | new ObjectClass(1, 2, 3);
| New Value Operator (new) |
An expression with the background operator | background function();
| Background Operator (background) |
An if statement | if (var == 3) {}
| if and else Statements |
An if ... else statement | if (var == 3) {} else {}
| if and else Statements |
A while statement | while (var < 10) {}
| while Statements |
A do while statement | do {} while (True);
| do while Statements |
A for statement | for (int i = 0; i < 10; ++ i) {}
| for Statements |
A foreach statement | foreach softint i in (list) {}
| foreach Statements |
A switch statement | string printf(string fmt,...) Outputs the string passed to standard output, using the first argument as a format string; does not e... | switch Statements |
A return statement | return val;
| return Statements |
A local variable declaration | string var;
my (int a, string b, bool c);
| Variables, Variable Declarations and Lexical Scope |
A global variable declaration | our int var;
our (float a, int b, hash c);
| Variables, Variable Declarations and Lexical Scope |
A function call | calculate(this, that, the_other);
| Functions, Object Method Calls, Static Method Calls, Closures, Call References |
A continue statement | continue;
| continue Statements |
A break statement | break;
| break Statements |
A statement block | {}
| zero or more statements enclosed in curly brackets |
A throw statement | throw "ERROR", description;
| throw Statements |
A try and catch statement | try and catch Statements | |
A rethrow statement | rethrow;
| rethrow Statements |
A thread_exit statement | thread_exit;
| thread_exit Statements |
A context statement | context top (q) {}
| context Statements |
A summarize statement | summarize (q) by (%date) where (%id != NULL) {}
| summarize Statements |
A subcontext statement | subcontext where (%type == "INBOUND" ) {}
| subcontext Statements |
An on_exit statement | on_exit l.unlock();
| on_exit Statements |
An on_success statement | on_success ds.commit();
| on_success Statements |
An on_error statement | on_error ds.rollback();
| on_error Statements |
if
statement allows for conditional logic in a Qore program's flow; the syntax is similar to that of C, C++, or Java.if
(
expression)
[else
]
else
keyword after the first statement, the following statement is executed.for
statement is most similar to the for statement in C and Java, or the non array iterator for statement in Perl. This statement is ideal for loops that should execute a given number of times, then complete. Each of the three expressions in the for statement is optional and may be omitted. To iterate through a list without directly referencing list index values, see the foreach statement.for
(
[initial_expression];
[test_expression];
[iterator_expression])foreach
statement is most similar to the for
or foreach
array iterator statement in Perl. To iterate an action until a condition is True, use the for statement instead.foreach
[my|our] [
type]
variable in
(
expression)
foreach
operator iterates the object by calling AbstractIterator::next(), and the values assigned to the iterator variable on each iteration are the container values returned by AbstractIterator::getValue().\lvalue_expression
) is used as the list expression, any changes made to the foreach
iterator variable will be written back to the list (in which case any AbstractIterator object is not iterated; references cannot be used with AbstractIterator objects as such objects provide read-only iteration).my or
our keywords
are required with new variables or their type has to be declared, otherwise a parse exception will be raisedswitch
(
expression) {
case
case_expression:
[default:
]
}
switch
statement.case
blocks until on of the previous statements is encountered or until the end of the switch
statement.Valid Case Expression Operators
Operator | Description |
> | Greater Than Operator (>) |
>= | Greater Than Or Equals Operator (>=) |
< | Less Than Operator (<) |
<= | Less Than Or Equals Operator (<=) |
== | Equals Operator (==) (with type conversions) |
=~ | Regular Expression Match Operator (=~) (in this case the regular expression may be optionally given without the operator) |
!~ | Regular Expression No Match Operator (!~) |
while
statements in Qore are similar to while statements in Perl, C and Java. They are used to loop while a given condition is True.while
(
expression)
do
while
statements in Qore are similar to do while statements in C. They are used to guarantee at least one iteration and loop until a given expression evaluates to False.do
while
(
expression);
do
while
statements and while statements is that the do
while
statement evaluates its loop expression at the end of the loop, and therefore guarantees at least one iteration of the loop.continue
; continue
statement affects loop processing; that is; it has an affect on for, foreach, while, do while, context, summarize, and subcontext loop processing.break
; break
statement affects loop processing; that is; it has an affect on for, foreach, while, do while, context, summarize, and subcontext loop processing as well as on switch block processing.throw
statement must be used.throw
expression;catch
block of a try/catch statement, if the throw
is executed in a try block. Otherwise the default system exception handler will be run and the currently running thread will terminate.try
and catch
statements have to be used. When an exception occurs while executing the try
block, execution control will immediately be passed to the catch
block, which can capture information about the exception.try
catch ([hash[<ExceptionInfo>]] [
exception_hash_variable])
catch
declaration, it will not be possible to access any information about the exception in the catch
block. However, the rethrow statement can be used to rethrow exceptions at any time in a catch
block.rethrow
statement can be used to rethrow an exception in catch
and on_error blocks. In this case a entry tagged as a rethrow entry will be placed on the exception call stack.rethrow
; rethrow
expression;catch
block, or to the system default exception handler, as with a throw statement.catch
block (for detailed information about the exception hash and the format of call stacks, see Exception Handling).rethrow
statement remain unchanged. Note that it is an error to use the rethrow
statement outside of a catch
block.thread_exit
statements cause the current thread to exit immediately. Use this statement instead of the exit() function when only the current thread should exit.thread_exit
; context
statement can be used. Column names can be referred to directly in expressions in the scope of the context statement by preceding the name with a "%"
character, while the current row can be referenced with %%
.context
[name] (
data_expression)
[where (
where_expression)]
[sortBy (
sort_expression)]
[sortDescendingBy (
sort_descending_expression)]
context
statement to execute.where
expression may be given, in which case for each row in the hash, the expression will be executed, and if the where expression evaluates to True, the row will be iterated in the context loop. If this expression evaluates to False, then the row will not be iterated. This option is given so the programmer can create multiple views of a single data structure (such as a query result set) in memory rather than build different data structures by hand (or retrieve the data multiple times from a database).sortBy
expression may also be given. In this case, the expression will be evaluated for each row of the query given, and then the result set will be sorted in ascending order by the results of the expressions according to the resulting type of the evaluated expression (i.e. if the result of the evaluation of the expression gives a string, then string order is used to sort, if the result of the evaluation is an integer, then integer order is used, etc).context
statement that behaves the same as above except that the results are sorted in descending order.summarize
statements are like context statements with one important difference: results sets are grouped by a by
expression, and the statement is executed only once per discrete by
expression result. This statement is designed to be used with the subcontext statement.summarize
(
data_expression) by (
by_expression)
[where (
where_expression)]
[sortBy (
sort_expression)]
[sortDescendingBy (
sort_descending_expression)]
summarize
statements modifiers have the same effect as those for the context statement, except for the following:by (
by_expression)
by
expression is executed for each row in the data structure indicated. The set of unique results defines groups of result rows. For each group of result rows, each row having an identical result of the evaluation of the by expression, the statement is executed only once.subcontext
[where (
where_expression)]
[sortBy (
sort_expression)]
[sortDescendingBy (
sort_descending_expression)]
subcontext
statement is used in conjunction with summarize statements. When result rows of a query should be grouped, and then each row in the result set should be individually processed, the Qore programmer should first use a summarize statement, and then a subcontext
statement. The summarize statement will group rows, and then the nested subcontext
statement will iterate through each row in the current summary group.return
statements causes the flow of execution of the function, method or program to stop immediately and return to the caller. This statement can take an optional expression to return a value to the caller as well.return
[expression];on_exit
on_exit
statement provides a clean way to do exception-safe cleanup within Qore code. Any single statment (or statement block) after the on_exit
keyword will be executed when the current block exits (as long as the statement itself is reached when executing - on_exit
statements that are never reached when executing will have no effect).on_exit
statement in the block is important, as the immediate effect of this statement is to queue its code for execution when the block is exited, meaning that on_exit
statements (along with on_success
and on_error
statements) are executed in reverse order respective their declaration when the local scope is exited for any reason, even due to an exception or a return statement. Therefore it's ideal for putting cleanup code right next to the code that requires the cleanup.on_exit
code will be executed for each iteration of the loop.on_success
on_success
statement provides a clean way to do block-level cleanup within Qore code in the case that no exception is thrown in the block. Any single statment (or statement block) after the on_success
keyword will be executed when the current block exits as long as no unhandled exception has been thrown (and as long as the statement itself is reached when executing - on_success
statements that are never reached when executing will have no effect).on_success
statement in the block is important, as the immediate effect of this statement is to queue its code for execution when the block is exited, meaning that on_success
statements (along with on_exit
and on_error
statements) are executed in reverse order respective their declaration when the local scope is exited for any reason, even due to an exception or a return statement. Therefore it's ideal for putting cleanup code right next to the code that requires the cleanup, along with on_error statements, which are executed in a manner similar to on_success
statements, except on_error
statements are only executed when there is an active exception when the block is exited.on_success
code will be executed for each iteration of the loop (as long as there is no active exception).on_error
on_error
statement provides a clean way to do block-level cleanup within Qore code in the case that an exception is thrown in the block. Any single statment (or statement block) after the on_error
keyword will be executed when the current block exits as long as an unhandled exception has been thrown (and as long as the statement itself is reached when executing - on_error
statements that are never reached when executing will have no effect).on_error
statement in the block is important, as the immediate effect of this statement is to queue its code for execution when the block is exited, meaning that on_error
statements (along with on_exit
and on_error
statements) are executed in reverse order respective their declaration when the local scope is exited for any reason, even due to an exception or a return statement. Therefore it's ideal for putting cleanup code right next to the code that requires the cleanup, along with on_success statements, which are executed in a manner similar to on_error
statements, except on_success statements are only executed when there is no active exception when the block is exited.$1
is set to the current active exception, and also rethrow statements are allowed in on_error
statements, allowing for exception enrichment. Note that the code in this statement can only be executed once in any block, as a block (even a block within a loop) can only exit the loop once with an active exception (in contrast to on_success and on_exit statements, which are executed for every iteration of a loop).