John and Milind,

function test() is declared in the current scope, and globally as filterform.test

After fiddling around for a bit, it looks like wx.wxEvtHandler:Connect saves upvalues, but not the current function environment (scope), which was set in the call to module(...) When the event function is called, the function environment is reset to the global scope, so if you want to reference things you declared in the module scope in the event callback, you have a couple of options:
1. Declare everything as local, which to me is just good practice.
2. Use a fully qualified name (e.g. filterform.test())
3. Wrap all your calls to wx.wxEvtHandler:Connect like this:

function Connect(evthandler, id, func)
    local environment = getfenv(1)
    evthandler:Connect(id,
        function(event)
            setfenv(1, environment)
            func(event)
        end
    )
end

[...]

local button = wx.Button([...])
Connect(button, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) end)

John -- It seems like wx.wxEvtHandler:Connect should do this anyways . . . that is, save the current function environment, and then apply it before calling the callback.

Mike

On 2/16/2012 3:14 AM, Milind Gupta wrote:
Hello John,
Sorry for the carelessness. Here is a full set with minimal.lua which is the main file to run and the module function is called by the Help->About call back. That is true making test a local function makes it work although I am not totally clear why but if you press the other button 'Clear Task' it will give an error complaining about filter which again is not declared local. Actually declaring local anything in a module only makes it private. Whether it is declared local or not it always resides in the module environment. So even if test or filter are not declared local they should be identified in the module environment. Here is the Programming in Lua Modules and Packages chapter that clarifies it:

http://www.inf.puc-rio.br/~roberto/pil2/chapter15.pdf <http://www.inf.puc-rio.br/%7Eroberto/pil2/chapter15.pdf>

At least that is how I understood it. So I think it should work without being declared local. In your previous email you said that the environment is saved as an upvalue with the execution of Connect. I am not sure how that would be done since the module itself does not know what environment to Global it will be in. Like if you see in minimal I placed it in the environment newPackage and all my functions tables declared in the module i.e. test, filter, etc. are in the table newPackage in the global environment but the module code has no way of knowing that. One last point is that unless its an event callback function normal functions in the module run just fine whether test is local or not. As an example I just added the test() call as the first line in filterFormActivate which is a non event function so when the Help-> About callback calls filterFormActivate, test() just runs fine even though it is not local but now if you press the Select Task button it will say it could not find test().

Milind


On Wed, Feb 15, 2012 at 3:14 AM, Milind Gupta <milind.gupta@...> wrote:
> Hello John,
>
> thanks for the reply. Here is an example code that does not work

Please try to provide enough code to make it run out of the box... I
had to add back GUI.initFrameH and rem out the usage of MainSizer to
make it even possible to run without error.

> (Pasted below and attached the file as well). A function from the main wxlua
> application calls filterFormActivate which sets up a simple frame with 2
> buttons and one static text and associates the button click to SelTaskPress.

I made the help menu callback function of the minimal.wx.lua sample
call filterform.filterFormActivate(frame).

> But when the button is clicked the test() statement gives an error. If that
> is removed then it cannot recognize the frame varaiable in the
> wx.wxFrame(frame line after test().

Please always include these errors in the future to help diagnose the problem.

In order to solve your problem I made test() a local function so that
it will be an upvalue of the Connect() function that calls it. Note
that the 'frame' that is used in the SelTaskPress() function is the
frame from the minimal.wx.lua and not the frame from
filterFormActivate().

To be honest, I have not done too much with modules and require in Lua
since I'm not sure I fully understand its behavior.

I just came across this which clears some things up for me. Note the
sentence "This means that any global functions are not visible by
default, which is why we had to create a local alias to print." This
is probably also why making test() local above fixed it. See also
package.seeall which might be useful to you.
http://www.luafaq.org/#T1.37

Hope this helps,
    John



------------------------------------------------------------------------------
Virtualization&  Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/


_______________________________________________
wxlua-users mailing list
wxlua-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxlua-users
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
wxlua-users mailing list
wxlua-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxlua-users

Reply via email to