1. For example, if I have a .lua file (declared in .toc and as <Script>), does WoW run code that is hanging freely in the .lua? In my case:

     require("klass");
    
     gratzor_prototype = class:new();
     gratzor_prototype:build({stuff});
     function Gratzor_Initialize()
         gratzor = gratzor_prototype:new();
     end
    

    Where "klass" is another file stored in the same directory. Stuff is just a placeholder I used to remove unnecessary info from this post. Is that valid (will run) or I need to put it inside the Gratzor_Initialize? (which is properly invoked from my XML's <OnLoad>)

    For example, if I have a .lua file (declared in .toc and as <Script>), does WoW run code that is hanging freely in the .lua? In my case:

     require("klass");
    

    Require doesn't exist in WoW, so this will not work.

     gratzor_prototype = class:new();
     gratzor_prototype:build({stuff});
     function Gratzor_Initialize()
         gratzor = gratzor_prototype:new();
     end
    

    This will be compiled and executed as your addon is loading.

    Where "klass" is another file stored in the same directory. Stuff is just a placeholder I used to remove unnecessary info from this post. Is that valid (will run) or I need to put it inside the Gratzor_Initialize? (which is properly invoked from my XML's <OnLoad>)

    No, you can definitely run code in the main chunk like this.

    So instead of using require() I should put klass.lua in the .toc file before the code that uses it and it will work?

    Thanks in advance, Kroltan Bahuman

    All files listed in your ToC will execute in order. If "file1" requires "file2", then you need to load "file2" first.

    The entire file body will be parsed and executed when the file loads. This includes all functions defined in the main body (not functions defined in other functions, though - those are defined when that function runs).

    You can essentially think of every file as its own function body - it has its own file scope (that locals are constricted to), and even gets passed two arguments using ... - arg1 is the addon's folder name, and arg2 is a table that will be the same for all files listed in the ToC.

    After all your files are loaded, initialization will at some undefined point in time before the loading screen is complete proceed to execute your addon's SavedVariables (lua) files. After this, the ADDON_LOADED event will fire with arg1 equal to your addon's folder name. If you want to do generic initialization (positioning saved frames, etc.), this is the time to do it as all your saved variables are guaranteed to be present and all your main body code (generic frame creation that does not require SVs) will have executed.

    Hope this clears things up somewhat.