Previously we discussed control structures in our scripting languages and we have touched on chunking up your code into procedures/functions/subroutines. Today we're going to take that a little further and discuss the ways in which our chosen languages group up those pieces of code into what is commonly referred to as a 'module'.

Conveniently, Python, Lua, and Perl all refer to collections of reusable code as modules so we don't need to learn a whole bunch of different words for the same concept today. However each language has different syntax (shocking, I know) for both: the creation of, and use of, modules.

Perl modules

Perl uses the Paamayim Nekudotayim to separate the scope of modules, so the Perl module which provides access to a useful piece of code designed to dump out data structures is called Data::Dumper. To use that in your own Perl code, you might do:

use Data::Dumper;

Perl is, as you might expect if you've gotten this far in our discussion of scripting languages, a little odd when it comes to building your own modules. Fortunately the Perl community is pretty awesome and they have written a very good simple module tutorial.

Python modules

Python uses the . to separate the scope of modules, so the Python module which provides you access to the system's path-related functions is called sys.path. To use that in your own Python code, you might do:

import sys.path

Python's module syntax is slightly less complex than Perl's: namely you just have a .py file and it's implicitly usable as a module straight off. Python extends this slightly by allowing for a special file called __init__.py which acts as the content of a module whose name is actually a directory. This allows you to group modules into what Python refers to as a package. You can learn more about this here.

Lua modules

Lua uses . as Python does. However Lua doesn't come with many modules by default, but if you happen to have it installed, then the Lua module containing code representing a multimap in the Penlight library of code is called pl.multimap and you would use it as follows:

local mm = require "pl.multimap"

Lua's module syntax is mid-way between Python's and Perl's. Unlike Python, you must explicitly return your module's contents, however unlike Perl you do not have to include a lot of other stuff in order to make it work. There's a reasonable Lua module tutorial available to help you with this.

Challenge

Your homework for this time, is to take some time to investigate what modules are available on your computer for the various languages you are playing along with, and see what they have to offer you. If you're feeling 'advanced' then you should proceed to write your own code module, perhaps containing some of the software you've written thanks to your experimentation in the previous installments of this series.