undeclared variables getting through in Strict Compilation mode

J. Landman Gay jacque at hyperactivesw.com
Tue Feb 17 01:14:55 EST 2015


On 2/16/2015 7:48 PM, Dr. Hawkins wrote:
> It used to be
>
> on prePreOpenStack
>     local prefsDir, prefsFilNam, newPrefsDb, prTabNams
>     local dcmd, theData, vtb
>
> ...
>     put "SELECT pref, pval FROM prefs" into dcmd
>     put revDataFromQuery(tab,vtb,prefsDb,dcmd) into prAry
>     split prAry by vtb and tab
>
>
> It threw no error loading prAry, and it turned into the correct array.
>
> Single step into the calling handler, and prAry and the global prAry was
> gone.

This is correct behavior, at least when explicit variables is off (if 
it's on, you shouldn't be able to compile at all.) An undeclared 
variable will be automatically created and will be local to that 
specific handler.

> Strict compilation was set in the preferences; I checked.  I can't use
> explicit variables without a significant code rewrite, as "long name"
> produces a result that is incompatible with, well, livecode.

I can't imagine why the long name would cause any trouble, but that's a 
different problem. Anyway, "strict compilation" is the same thing as 
explicit variables. Preferences just gives it a different, presumably 
more descriptive, name.

The issue here seems to be that you had explicit variables turned on but 
you didn't get a warning about the undeclared global variable.

>
> I added "global prAry" after the local declarations, and everything worked
> fine

Right. A global declared inside a handler can only be accessed within 
that handler. If you want to see and use it in other handlers, you need 
to declare it in each one where it's used. That's why when you stepped 
out of the handler it seemed to disappear. If you had checked "the 
globals" it should have been there, but you wouldn't see it in the 
variable watcher because it isn't available to that particular handler 
without a declaration.

The alternative is to declare it once outside of all handlers, where it 
will be available to all of them below the declaration.

So, if I understand right, you had explicit variables turned on but you 
didn't get a flag about the undeclared global. Instead, the editor acted 
as though explicit variables was off and automatically created a local 
variable. It may have something to do with the contortions the engine 
goes through to prevent conflicts with libraries that don't declare 
variables. (Richard G. discussed that a few days ago.) Basically it 
needs to turn off explicit variables, compile, and then reset it again 
to your preference setting.

Without that, people who don't use explicit variables couldn't employ 
any libraries written by people who do.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list