lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

Penlight is a set of general-purpose libraries; the core was inspired
by the Python standard library, but has since expanded out in more
independent directions, for example reading configuration files with
pl.config, table data with pl.data, and XML pretty-printing and
construction with pl.xml.

Version 1.1 is mostly a bug fix release with a few new intriguing
ffeatures. It's already available through LuaRocks, and a full zipball
with tests and documentation source is available here [1].

Documentation (both module and manual) is online here [2]

Many thanks to Dennis Schriddle,  Thijs Schreijer and Andrew Starks
(among others) for being the eyeballs in the cloud.

Andrew has generalized the 'load all' option in Penlight in a way
which is less all-or-nothing. You can for instance load everything
into a single table with

local pl = require 'pl.import_into' ()

And then pl.utils, pl.tablex, etc will be loaded on demand, with no
globals being harmed. (in fact require 'pl' is implemented now using
pl.import_into)

A variation allows this kind of convenience for Lua 5.2 modules:

local _ENV,M = require 'pl.import_into' (true)

function answer ()
    -- all the Penlight modules are available!
    return pretty.write(utils.split '10 20  30', '')
end

return M

Note that M is not necessarily _ENV!  _ENV contains _anything you
like_, while M _only_ contains the functions/fields/etc defined inside
the module.  (If it was called with 'false' then _ENV==M and we have a
classic leaky module)

pl.strict has also been generalized; loading this modules enforces the
classic 'undeclared globals are errors' rule,  If now I define a
module with strict.module, you get the same kind of errors for modules
as well:

-- mymod.lua
local strict = require 'pl.strict'
local M = strict.module (...)

function M.answer ()
    return 42
end

function M.question ()
    return 'what is the answer to Life, the Universe and Everything?'
end

return M

Any attempt at misspelling (e.g. mymod.Answer()) will create a clear
runtime error. This function can be applied to existing modules, and
strict.make_all_strict(_G) will make all the standard libraries behave
in the same way.

pl.utils continues to provide 5.1/5.2 compatibility (and you can of
course just use this module for compatibility); it always defines
utils.load(), which behaves like 5.2. Slightly more controversially,
it defines set/getfenv as globals in 5.2 using Sergey Rozhenko's
method of carefully identifying the _ENV upvalue.  (I'm thinking that
utils.setfenv would be more consistent with the 'do not clobber
globals rule')

Although a fairly big library, over the last few years I've reduced
the inter-module dependencies to the point where many of them only
depend on pl.utils. This will help those who are averse to kitchen
sink code reuse.

steve d.

[1] http://stevedonovan.github.com/files/penlight-1.1.zip
[2] http://stevedonovan.github.com/Penlight/api/index.html