lua-users home
lua-l archive

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


steve donovan wrote:

On Mon, Apr 22, 2013 at 9:25 PM, joao lobato <btnfdp.lobato@gmail.com
<mailto:btnfdp.lobato@gmail.com>> wrote:

    So, you want functions...


Heh, well put!  It would be nice if dofile() was allowed to pass
arguments to the main chunk. E.g. we have 'dofile.lua' which is just:

print (...)

dofile() won't pass any arguments  - but it's easy to write a dofile
which does take arguments:

> c = loadfile 'dofile.lua'
> c('one',1,20)
one     1       20

BTW, there is a little diffference between require() on 5.1 and 5.2:

> require 'dofile'
dofile  .\dofile.lua

That is, also passes the filename as well as the module name.

steve d.




Right, but what I was wishing for was the ability to name the parameters passed to the function returned by load()

An example of the usage of this can be seen along the lines of HTML event handlers: Imagine an excerpt from a HTML document using Lua instead of _javascript_:
   <button _onclick_="table.insert(eventLog, ev)">Name</button>
Browsers use the _javascript_ functionality to load a fragment of code as a function with named parameters to inject variables like "ev" into the function's scope.