2.7.1 Introduction to the NAMEREF mechanism

With these facilities we can use the _var variants of MBFL functions as follows:

mbfl_declare_varref(RV)

mbfl_file_extension_var mbfl_datavar(RV) /path/to/file.ext
"$RV"  ⇒ ext

without risking a variable name collision and without using the standard output of callee functions to acquire a result. In this example, the use of mbfl_declare_varref() expands into:

declare mbfl_a_variable_RV
mbfl_variable_alloc mbfl_a_variable_RV
declare $mbfl_a_variable_RV
declare -n RV=$mbfl_a_variable_RV

a name variable mbfl_a_variable_RV is declared and filled with a unique data variable name by mbfl_variable_alloc(); then the data variable is declared; finally a proxy variable RV is defined as alias for the data variable.

Remember that when using declare: if we are at the top–level, the variable will be global; if we are in a function, the variable will be local.

Given a proxy variable name defined this way: we can retrieve its name variable name with the macro mbfl_namevar(); we can retrieve its data variable name with the macro mbfl_datavar().

Other macros declare variables similarly, using the same scheme of 3 variable declarations: name variable, data variable, proxy variable.

If we declare a data variable as global: we can pass its name around and accessing it into other functions; we have to remember to release it somewhere.

Yes: it’s madness…


This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.