Functions: Namespacing

When your script becomes more complex with the addition of library script code, the problem of function and variable name collision comes up. Again the yoctu project comes to my rescue, as it shows an interesting use of non alpha-numeric identifier characters in function names.

Take the :: notation from C++ and change it a little so it matches the form of <executable>::<library>::<function>. As an example, if an executable “foo” has a library script file “bar.sh”, any function in “bar.sh” will have a foo::bar:: prefix:

foo::bar:do_something()
{
   local text="$1"

   printf "%s\n" "${text}"
}

The include function can be easily extended to parse this foo::bar prefix. So actual calling foo::bar::do_something from any script code will look like this:

include "foo::bar"

foo::bar:do_something "hello"

That’s quite readable and powerful to boot.