Use Explicit Namespaces
Including library files feels like copy and paste with extra steps. It allows you to move quickly at the beginning, but eventually you start to notice that you struggle to understand where code came from, which means struggling to find things when you look for them. Yes, grep and its cousins help, but it feels better to have an idea what’s what and where it came from. Namespaces help.
From Libraries to Modules
When you include code from library files, you can turn them into modules imported into namespaces with just a dash of extra code. It’s never worth the effort until it very suddenly is.
echo "\"CAD 175.62\"" | jq -L "assets/jq" 'import "parsing" as Parsing; Parsing::parse_monetary_amount'We have done this:
- Replaced
includewithimport. - Specified the namespace
Parsingwhen weimport "parsing". - Specified the namespace when we invoke the library function:
Parsing::parse_monetary_amount. - Specified the library path so that
jqcan find theparsingmodule.
That’s enough! Now you can import functions from two libraries with the same name without a naming collision. There is a lot more to modules than this, as the jq manual explains, but frankly I haven’t yet needed to know any more than what you’ve seen here. This is truly enough.