github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/private.md (about) 1 # `private` 2 3 > Define a private function block 4 5 ## Description 6 7 `private` defines a function who's scope is limited to that module or source 8 file. 9 10 Privates cannot be called from one module to another (unless they're wrapped 11 around a global `function`) and nor can they be called from the interactive 12 command line. The purpose of a `private` is to reduce repeated code inside 13 a module or source file without cluttering up the global namespace. 14 15 ## Usage 16 17 ``` 18 private name { code-block } 19 ``` 20 21 ## Examples 22 23 ``` 24 # The following cannot be entered via the command line. You need to write 25 # it to a file and execute it from there. 26 27 private hw { 28 out "Hello, World!" 29 } 30 31 function tom { 32 hw 33 out "My name is Tom." 34 } 35 36 function dick { 37 hw 38 out "My name is Dick." 39 } 40 41 function harry { 42 hw 43 out "My name is Harry." 44 } 45 ``` 46 47 ## Detail 48 49 ### Allowed characters 50 51 Private names can only include any characters apart from dollar (`$`). 52 This is to prevent functions from overwriting variables (see the order of 53 preference below). 54 55 ### Undefining a private 56 57 Because private functions are fixed to the source file that declares them, 58 there isn't much point in undefining them. Thus at this point in time, it 59 is not possible to do so. 60 61 ### Order of preference 62 63 There is an order of precedence for which commands are looked up: 64 65 1. `runmode`: this is executed before the rest of the script. It is invoked by 66 the pre-compiler forking process and is required to sit at the top of any 67 scripts. 68 69 1. `test` and `pipe` functions also alter the behavior of the compiler and thus 70 are executed ahead of any scripts. 71 72 4. private functions - defined via `private`. Private's cannot be global and 73 are scoped only to the module or source that defined them. For example, You 74 cannot call a private function directly from the interactive command line 75 (however you can force an indirect call via `fexec`). 76 77 2. Aliases - defined via `alias`. All aliases are global. 78 79 3. Murex functions - defined via `function`. All functions are global. 80 81 5. Variables (dollar prefixed) which are declared via `global`, `set` or `let`. 82 Also environmental variables too, declared via `export`. 83 84 6. globbing: however this only applies for commands executed in the interactive 85 shell. 86 87 7. Murex builtins. 88 89 8. External executable files 90 91 You can override this order of precedence via the `fexec` and `exec` builtins. 92 93 ## See Also 94 95 * [`alias`](../commands/alias.md): 96 Create an alias for a command 97 * [`break`](../commands/break.md): 98 Terminate execution of a block within your processes scope 99 * [`exec`](../commands/exec.md): 100 Runs an executable 101 * [`export`](../commands/export.md): 102 Define an environmental variable and set it's value 103 * [`fexec` ](../commands/fexec.md): 104 Execute a command or function, bypassing the usual order of precedence. 105 * [`function`](../commands/function.md): 106 Define a function block 107 * [`g`](../commands/g.md): 108 Glob pattern matching for file system objects (eg `*.txt`) 109 * [`global`](../commands/global.md): 110 Define a global variable and set it's value 111 * [`let`](../commands/let.md): 112 Evaluate a mathematical function and assign to variable (deprecated) 113 * [`method`](../commands/method.md): 114 Define a methods supported data-types 115 * [`set`](../commands/set.md): 116 Define a local variable and set it's value 117 * [`source`](../commands/source.md): 118 Import Murex code from another file of code block 119 120 <hr/> 121 122 This document was generated from [builtins/core/structs/function_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/structs/function_doc.yaml).