Basics
Users familiar with Python will feel at home with Mpl. Mpl supports syntax and basic types as in Python. Further, some unique language extensions and capabilities make Mpl more potent for the target use cases. We will learn those in subsequent sections. Below is a peek into what a typical interactive user session in Mpl repl looks like.
Mpl is available on Linux, MacOs and Windoes. mpl
can be launched on any modern terminal. mpl -h
prints supported command line options.
For a typical trouble shooting session, one can issue Mpl "commands" on repl and assign the output to a variable. That variable (typically a Json document) is available as input to next command. More details in subsequent section. For commands (in mpl they start with !
) convenient command completion options is available as user types.
> ./mpl -i '{"site":"10.1.1.214"}'
mpl> x = ! k8s list-pods --namespace "customer1"
mpl> print x[0].status
"Running"
mpl>
Mpl interpreter in batch mode is invoked with mpl file in shell.
Types
Mpl suppoprts types such as Number, String, Boolean, Json, Time, and Duration. Note that, unlike other languages, Mpl has Json, Time, and Duration as first-class built-in types. Refer to the 'types' section for details on each type.
Variable
-
Simple variable: A simple variable in Mpl can be defined similar to python Python. e.g.,
The value of
x
is2
. -
Indexed variable: Mpl support intuitive indexed variable to access values in JSON documents. Users can create JSON documents via the keyword
output is:json
followed by a valid JSON text. To access any field in the document, one can use syntax likex.y[1]["a"]
or in more convenient dotted notation,x.y[1].a
. e.g.,1
.
Expression
In addition to simple assignment of 'rhs' variable to 'lhs', 'rhs' can be an expression. Expression can be composed of basic opertors like +
, -
, *
, /
and builtin operators like 'len' and methods. For full documentation of methods supported for each type, refer to 'types' section.
Examples:
Example 1: Simple arithmetic
output is: 12
Example 2: Simple string transformation
output is: "bluedot"
Condition
Mpl supports Python like if-else
branching syntax. The comparison expression can be on any Mpl supported data types. The supported compare operators are ==
, !=
, >
, <
, >=
, <=
and in
. In addition if not
statement supported. e.g.,
x
will be "white"
.The conditional expression can be chained by
&&
and ||
operators. Unix like wildcard match, specified in backtick, is also supported. e.g.,
Here vlaue of x
will be "cat-or-dog"
.
The in
operator find existance of value in arrays or a key in object. e.g.,
Here vlaue of res
will be true
.
Loop
Mpl supports for
loop syntax with simple loops over integer or iterables (like Json doc). Note that Python's indentation rule for the loop block applies. For example in following simple loop:
output is:
As a shortcut, if indexing variablei
is not needed, in Mpl, for x:
is a valid statement to loop for x
times. Also break
and continue
are supported, shown below.
For iterables like Json array, value will be assigned as Json value of iterable. Refer to Json type section for more details on Json.
output is:
In case of Json object, index variable,i
, is assigned to key
of map (sub document). e.g.,
output is:
Function
Mpl supports Python like function definition. Functions have read only access to all variables from calling scope. At this time, unlike Python, Mpl functions can return only one variable. e.g.,
output is:
Example 2:
output is: