lua-users home
lua-l archive

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


It was thus said that the Great Peter Hickman once stated:
> Thanks for all the responses, I am working my way through the various links
> to see what works for me. They are all interesting. But as a side issue I
> ended up with the following as the least technical solution
> 
> local blob_data = {}
> blob_data['game.lua'] = [[.. file as a string ..]]
> blob_data['show.lua'] = [[.. file as a string ..]]
> function require(name)
> local fullname = name .. '.lua'
> assert(blob_data[fullname], 'File not found: ' .. fullname)
> local code = load(blob_data[fullname])
> return code()
> end
> require('game')

  You could do:

	do
	  local blob_data = {}
	  blob_data['game.lua'] = [[ ... file as a string ... ]]
	  blob_data['show.lua'] = [[ ... file as a stirng ... ]]

	  -- --------------------------------------------
	  -- if using Lua 5.1, replace
	  -- 	package.searchers with package.loaders
	  --	load() with loadstring()
	  -- --------------------------------------------

	  table.insert(package.searchers,1,function(name)
	    local fullname = name .. '.lua'
	    if not blob_data[name] then
	      return string.format("\n\tmodule %q not found",name)
	    else
	      return load(blog_data[fullnaem])
	    end
	  end)
	end

	require "game"

and not change the require() function at all.  Using the 'do' here at the
top creates a new scope in which you can hide blob_data[], and you are
adding a function to the package.searchers[] array, which means it will
still do the normal Lua searching as a fall back.  

  -spc