Don't understand the meaning of "local"

Jim Ault JimAultWins at yahoo.com
Fri Dec 23 18:23:06 EST 2005


On 12/23/05 12:55 PM, "Jim Hurley" <jhurley at infostations.com> wrote:

> The persistence of local SCRIPT variables that Chipp's earlier reply
> clears up for me is handlers calling themselves (in x millisec or
> whatever) in  order to achieve asynchronous behavior ( i.e. not
> monopolizing CPU  time and allowing other uses of the  CPU to seek
> in.)

Yes, and since there are two types of LOCAL variables, one could adopt the
naming convention:

g = global, persistent and available to any handler in any script container.
s = script local, persistent but only available for handlers in that script
container
t = temporary or handler local
p= passed parameter, same as 't'.  Important: this is a new variable
containing a copy of the original contents.  Any changes you make to a 'p'
variable will not change the original.  To do that, you need to 'return
pWhatever' and put it into the original variable.

thus there could be the following script:
[make an new main stack, paste into the stack script, change to browse mode,
and double click, follow the bouncing dialog]
------- start copy here -------------
--notes at the end
global gGristForOneAndAll, gMoreGrist

local sBagOfCandy ="5"
local sPrevCard, sLastLineUsed, sLastMenuChoice
sNien ="9"  --nein, arbeitet nicht

on initializeVariables
  --initializeVariables
  --no need for global declaration while in this script container
  put "four pounds of grist" into gGristForOneAndAll
  put "gum drop, candy cane, peppermint " into sBagOfCandy
  put the number of this card into sPrevCard
  put "So what's your sign" into sLastLineUsed
  put "Surf n' turf" into sLastMenuChoice
  put "gone in a moment" into sNien
end initializeVariables

on mouseDoubleUp
  --if gGristForOneAndAll is empty then initializeVariables
  initializeVariables
  testThis
end mouseDoubleUp

on testThis
  answer gGristForOneAndAll
  answer sBagOfCandy
  delete last item of sBagOfCandy
  answer sBagOfCandy
  answer sPrevCard
  answer sLastLineUsed
  answer sLastMenuChoice
  answer sNein

  put testVarsHere() into tReturnedVal
  answer tReturnedVal
  put testVarsHere(" Cindy Lou Who") into tReturnedVal
  answer tReturnedVal
  answer pWhovillian --and not the value
  answer tCaption --and not the value
end testThis

function testVarsHere pWhovillian
put "same script, different handler" & pWhovillian & cr into tCaption
answer tCaption & gGristForOneAndAll
answer tCaption &  sBagOfCandy
delete last item of sBagOfCandy
answer tCaption &  sBagOfCandy
answer tCaption & sPrevCard

--we have used a local temp var = tCaption, good only here
--we have used a parameter var = pWhovillian, good only here
put "Why-what-Where" into the last word of pWhovillian
return "and now... back to the prev handler " & pWhovillian
--return "and now... back to the prev handler"
end testVarsHere 

--and now that this script has been used, the variables
--   gGristForOneAndAll, sBagOfCandy, sBagOfCandy, sPrevCard
--  are live and keep their current values
--  tCaption,  pWhovillian  have been deleted, don't exist, not just empty

------------- end copy


Jim Ault
Las Vegas















More information about the use-livecode mailing list