pages tagged luayakkinghttp://yakking.branchable.com/tags/lua/yakkingikiwiki2016-03-23T12:00:17ZScripting languages - packaging your moduleshttp://yakking.branchable.com/posts/scripting-packaging/Daniel Silverstone2016-03-23T12:00:17Z2016-03-23T12:00:09Z
<p>Last time, we discussed the grouping of code together into
<a href="http://yakking.branchable.com/posts/scripting-modules/">modules and packages</a> and at the end I encouraged you to investigate your
computer for software modules and also to have a go at putting your own modules
together.</p>
<p>Today we're going to look into the ways in which our chosen languages offer
other people's code modules for your delight and enjoyment. This is typically
referred to as a language's package database and it is one of the many ways of
getting hold of modules written by others without use of your operating
system's package manager.</p>
<h1>Perl</h1>
<p>Perl is a very well established langauge and community. For a long time, the
go-to place for Perl modules has been <a href="http://www.cpan.org/">CPAN</a> (The Comprehensive Perl Archive
Network). Perl comes with a special module <em>called</em> <code>CPAN</code> which can be used
to access the archive of modules without needing any other tooling. You can
interact with it by running:</p>
<pre><code>$ perl -MCPAN -eshell
</code></pre>
<p>Follow through the configuration, let it have a jolly good fiddle and then
it'll let you search for and install perl modules on your computer.</p>
<h1>Python</h1>
<p>Python's community went for a commandline tool rather than a REPL, and then
being a super-special community they schismed a bit and so there's both <a href="https://en.wikipedia.org/wiki/Pip_(package_manager)">pip</a>
and <a href="https://wiki.python.org/moin/EasyInstall">easy_install</a> to choose from. You can install Python modules as simply
as:</p>
<pre><code>$ pip install <packagename>
</code></pre>
<h1>Lua</h1>
<p>Lua, having a much smaller and to some extent younger community around it has
only managed to grow a single package manager. It is called <a href="https://luarocks.org/">luarocks</a> and
is closer in behaviour to <code>pip</code> than to <code>CPAN</code>.</p>
<pre><code>$ luarocks install <packagename>
</code></pre>
<p>is all you need to get something installed for use with Lua.</p>
<h1>Challenge</h1>
<p>Today's task is to become comfortable using the package managers of the
languages of your choice. Learn how to install and remove packages, and how to
search and retrieve information about different packages in the respective
databases. Then think of something fun to play with, install those packages,
and have a jolly good time coding with the libraries you didn't have to write
yourselves.</p>
Code modules in scripting languageshttp://yakking.branchable.com/posts/scripting-modules/Daniel Silverstone2016-03-02T12:00:17Z2016-03-02T12:00:11Z
<p><a href="http://yakking.branchable.com/posts/scripting-control/">Previously</a> we discussed control structures in our scripting languages and
we have touched on <a href="http://yakking.branchable.com/posts/scripting-chunks/">chunking up your code</a> 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'.</p>
<p>Conveniently, Python, Lua, and Perl all refer to collections of reusable code
as <em>modules</em> 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.</p>
<h1>Perl modules</h1>
<p>Perl uses the <a href="https://en.wikipedia.org/wiki/Scope_resolution_operator">Paamayim Nekudotayim</a> 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 <code>Data::Dumper</code>. To use that in your own
Perl code, you might do:</p>
<pre><code>use Data::Dumper;
</code></pre>
<p>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 <a href="http://www.perlmonks.org/?node_id=102347">simple module tutorial</a>.</p>
<h1>Python modules</h1>
<p>Python uses the <code>.</code> to separate the scope of modules, so the Python module
which provides you access to the system's path-related functions is called
<code>sys.path</code>. To use that in your own Python code, you might do:</p>
<pre><code>import sys.path
</code></pre>
<p>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 <code>__init__.py</code> 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
<a href="https://docs.python.org/2/tutorial/modules.html#packages">here</a>.</p>
<h1>Lua modules</h1>
<p>Lua uses <code>.</code> 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 <a href="https://en.wikipedia.org/wiki/Multimap">multimap</a> in the
<code>Penlight</code> library of code is called <code>pl.multimap</code> and you would use it
as follows:</p>
<pre><code>local mm = require "pl.multimap"
</code></pre>
<p>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 <a href="http://lua-users.org/wiki/ModulesTutorial">Lua module tutorial</a>
available to help you with this.</p>
<h1>Challenge</h1>
<p>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.</p>