How scoping works within Murex
A scope in Murex is a collection of code blocks to which variables and config are persistent within. In Murex, a variable declared inside an if
or foreach
block will be persistent outside of their blocks as long as you’re still inside the same function.
For example lets start with the following function that sets a variable called foo
function example {
if { true } then { set foo=bar }
out $foo
}
In here the value is getting set inside an if
block but its value is is retrieved outside of that block. out
and set
have different parents but the same scoping.
Then lets set foo outside of that function and see what happens:
» set foo=oof
» $foo
oof
» example
bar
» $foo
oof
Despite setting a variable named foo, the value inside example does not overwrite the value outside example because they occupy different scoping.
A new scope is instantiated by anything which resembles a function. This would be code inside events, dynamic autocompletes, open agents, any code blocks defined in config
, as well as public and private functions too.
Code inside an if
, switch
, foreach
and source
do not create a new scope. Sub-shells also do not create a new scope either.
open
” (openagent
): Creates a handler function for open
set
): Define a variable (typically local) and set it’s valueforeach
): Iterate through an arrayif
): Conditional statement to execute different blocks of code depending on the result of the conditionsource
): Import Murex code from another file or code blockevent
): Event driven programming for shell scriptsprivate
): Define a private function blockfunction
): Define a function blockconfig
): Query or define Murex runtime settingsswitch
): Blocks of cascading conditionalsautocomplete
): Set definitions for tab-completion in the command linelet
: Evaluate a mathematical function and assign to variable (deprecated)This document was generated from gen/user-guide/scoping_doc.yaml.
This site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Wed Sep 18 21:18:57 UTC 2024 against commit c037883c03788357164e9846c84d9f777251495d9452a8e.
Current version is 6.3.4225 (develop) which has been verified against tests cases.