initialize an static, local, script-wide variable to empty

erik hansen erikhans08 at yahoo.com
Fri Apr 11 22:21:01 EDT 2003


--- Richard Gaskin <ambassador at fourthworld.com>
wrote:

> What was the emprical method used?
i repeated this handler:

global sHolder
on e
   put sHolder into msg
   # sHolder was always empty
   put 12 into sHolder
end e
   # the 12 did not stay in sHolder after <end e>

> Declared variables are initialized when a
> script loads, but once the script
> has loaded all other actions on that variable
> need to happen inside of handlers.
> 
> This bit about loading scripts is useful to
> keep in mind when debugging:
> any script-local variable will be
> re-initialized whenever you save or apply
> a script.
> 
> With a slightly limited range of expression
> types (constants only, no
> functions), you can initialize a script-local
> variable  outside of a handler
> in its declaration, as your example shows.
> 
> You should be able to display its value at
> runtime in the Message Box or an
> Answer dialog, but if you edit the script just
> keep in mind that it will be
> re-initialized.

just using the Debugger also has some weird side
affects.

> I tend to use them in two circumstances:
> 
> - where I might have used globals but want to
> avoid name-space collisions
> (great for utilities and plugins where you have
> no control over the name space)

"name space" is a new expression for me. ???

> - when passing data to handlers, as you
> suggest, where the same container is
> used among many handlers (except those that
> require their own copy or alter
> it ways that would be undesirable elsewhere).

after all this, i am back to declaring
temporary/in-handler locals. a trade off
between extra text versus having all variables
declared at the beginning of each handler.
using handler + parameters to pass info
involves the same kind of trade off.

using the script/static approach generated
some very strange results:

local sHolder = empty # no quotes
put the actual "e-m-p-t-y" string into sHolder
in the first handler that ran
the only instance i have ever seen where empty
did not resolve to just that.

there was a loop where:

local i # outside any handlers
on xxx
  repeat with i = 14 to 18
     # on the first pass (i = 14) fine,
     # on the second pass (i = 5)
     put goGetum() after sHolder
  end repeat
end xxx

function goGetum
  repeat with i = 1 to 5
    doSomething
  end repeat
end goGetum 

this is the only case in my experience where
repeat with i = 
did not follow orders

take home thought:
use script/static locals very carefully

=====
erik at erikhansen.org    http://www.erikhansen.org

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com



More information about the use-livecode mailing list