title: lua module is not loading libraries
body: <h2>Background Information:</h2>
  
  <p>I'm just new to lua and I'm trying to understand how modules work. But I'm trying to load a pre-existing module into a new script and run this script from the command line. </p>
  
  <h2>Code:</h2>
  
  <p>I have a file called main.lua module that looks something like this:</p>
  
  <pre><code>module (..., package.seeall)
  -- Load libraries
  require("luasql.postgres")
  require("luasql.sqlite3")
  
  local connect_to_db = function()
     if not global_con then
        env = assert (luasql.postgres())
        global_con = assert (env:connect(databasename, databaseUser, databasepassword, databaseserver))   
        return true
     else
        return false 
     end
  end
  
  update_widget = function (parm1, parm2, parm3)
    local connected = connect_to_db()         
    if connected then
       -- do something else
       return true
    end
  end -- end function.
  </code></pre>
  
  <p>I'm now trying to create a test script for this module.  I have the following logic in a separate lua file: </p>
  
  <pre><code>package.path = '/usr/share/myapp/main.lua;'
  local my_object = require("main")
  
  print my_object.update_widget
  </code></pre>
  
  <h2>Problem:</h2>
  
  <p>I'm getting the following error when I try to run my test script: </p>
  
  <pre><code>attempt to call field 'postgres' (a table value)
  </code></pre>
  
  <p>The line it's failing on is in the connect_to_db() method where I try to create an environment variable:</p>
  
  <pre><code>env = assert (luasql.postgres())
  </code></pre>
  
  <h2>What I've Tried so far:</h2>
  
  <ol>
  <li><p>I've modified the <code>package.path</code> in my test script to match what is being used by main.lua.  I did so by executing main.lua the "regular" way - driven by a web app - and dumping out the contents of <code>package.path</code> to a log file. 
  I've copied the path from the log file and used it as the <code>package.path</code> value in my test script... of course, I had to modify it by adding an additional entry - a path leading to main.lua.<br>
  But other than that, the package paths are the same. </p></li>
  <li><p>I've added print statements inside main.lua to prove that it is getting into the update_widget method... and that it's just failing trying to create the postgres.</p></li>
  <li><p>I've added the luasql.postgres library in the test script to see if that would help... like so:</p>
  
  <pre><code>package.path = '/var/x/appname/main.lua;'
  
  local pgdb = require("luasql.posgres")
  
  print(pgdb)
  myenv = assert(lua.postgres()) -- fails
  </code></pre></li>
  </ol>
  
  <p>The test script also dies trying to create this object... I'm going to keep hunting around.  It must be a problem with the paths... but I can't see the difference between the path thats created when loaded by the web app, vs. what I have in the test script. 
  I'm going to use a DIFF tool to compare for now. </p>
  
  <p>Any suggestions would be appreciated. </p>
  
  <p>Thanks. </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I definitely think it's the path, although I can't see just yet what's wrong with here. 
  I created yet another test script (let's call it test3).. but this time, I didn't explicitly  set the path by assigning values to package.path. 
  I just tried to include the luasql.postgres pacakge and use it the way the original test script does... and it works!
  So here's code that works: </p>
  
  <pre><code>luasql = require "luasql.postgres"
  local myenv = assert (luasql.postgres())
  print(myenv)
  </code></pre>
  
  <p>But this fails: </p>
  
  <pre><code>package.path = package.path .. ';/usr/share/myapp/main.lua'
  
  luasql = require "luasql.postgres"
  myenv = assert (luasql.postgres())
  print(myenv)
  </code></pre>
  
  <p>Also to greatwolf's point, I tried from interactive mode in lua... and my code works just fine. </p>
  
  <pre><code>Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
  &gt; pgdb = require("luasql.postgres")
  &gt; print(pgdb)
  table: 0x176cb228
  &gt; myenv=assert(luasql.postgres())
  &gt; print(myenv)
  PostgreSQL environment (0x176c9d5c)
  &gt; 
  </code></pre>
  
  <p>So... here's the package.path variable from interactive mode:</p>
  
  <pre><code>&gt; print(package.path)
  ./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua
  &gt; 
  </code></pre>
  
  <p>And here's the path from my original test script where it fails.  </p>
  
  <blockquote>
    <p>/usr/share/myapp/main.lua;./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua</p>
  </blockquote>