Skip to content

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.

> mpl
mpl> x = "fruit"
mpl> x + " is good for you"
"fruit is good for you"

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.

> mpl <file>

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

  1. Simple variable: A simple variable in Mpl can be defined similar to python Python. e.g.,

    x = 2
    x
    2
    

    The value of x is 2.

  2. Indexed variable: Mpl support intuitive indexed variable to access values in JSON documents. Users can create JSON documents via the keyword json followed by a valid JSON text. To access any field in the document, one can use syntax like x.y[1]["a"] or in more convenient dotted notation, x.y[1].a. e.g.,

    x = json {"b", [1, 3]}
    x.b[0]
    
    output is: 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

x = 20
y = x + x / 10
y

output is: 12

Example 2: Simple string transformation

x = "Blue"
x.lower() + "dot"

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 = "cat"
if x == "cat":
  x = "white"
else:
  x = "black"     
Here vlaue of 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.,

x = "cat"
y = "dog"
if x == `*c` && y == "dog":
  x = "cat-or-dog"
else:
  x = "nopet"     

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.,

x = json ["a", "b", {"c": 1, "d":2}]
if "a" in x && "c" in x[2]:
  res = true

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:

x = 2
for i in x:
   print i

output is:

0
1
As a shortcut, if indexing variable i 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.

x = json [2, 3, 5]
for val in x:
   if val == 5:
      continue
   print val

output is:

2
5
In case of Json object, index variable, i, is assigned to key of map (sub document). e.g.,

x = json {"a":1, "b":2, "c":5 }
for i in x:
   print i
....

output is:

"a"
"b"

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.,

def add():
    x = x + 1
    return x
x = 1
y = add()
y

output is:

2

Example 2:

def add(x):
    a = 10
    if a == 10:
       x = x + 2
    else:
       x = x + 4
    return x
p = 23
y = add(p)
y

output is:

25