Re: Scope of globals, parents and children, "load script"
Re: Scope of globals, parents and children, "load script"
- Subject: Re: Scope of globals, parents and children, "load script"
- From: "Marc K. Myers" <email@hidden>
- Date: Sat, 19 May 2001 23:23:02 -0400
- Organization: [very little]
>
Date: Sat, 19 May 2001 15:05:43 +1000
>
Subject: Re: Scope of globals, parents and children, "load script" an
>
From: Timothy Bates <email@hidden>
>
To: Scott <email@hidden>, AppleScriptUsers List
>
<email@hidden>
>
>
On 5/18/01 4:52 PM, "email@hidden" <email@hidden> wrote:
>
> Any handler (which includes the implicit run handler of the main script) that
>
> wants to use a global variable must declare it as such. The philosophy is
>
> that an outsider can't steal a handler's variables. That is, an outside bunch
>
of
>
> code should not be able to change the meaning of code inside a handler by
>
> declaring a variable global.
>
>
>
> If I write,
>
>
>
> on foo(x)
>
> set i to 0
>
> set x2 to x & x
>
> set x3 to x2 & bar(x)
>
> return x3 & (i as string)
>
> end foo
>
>
>
> Then it is a good thing that foo() doesn't unintentionally destroy the global
>
> variables i, x2, and x3, just because some code outside foo() declared "global
>
> x2". Its also good that bar() can't reach into foo() modify i when bar() is
>
> called.
>
>
Isn't that exactly what happens? For example in the code below, my global
>
"i" has been set to 0 and was accecssed implicitly by bar() without be
>
declared.
>
>
global i
>
set i to 100
>
>
foo(1)
>
>
return i
>
-->0
>
>
on foo(x)
>
set i to 0
>
set x3 to bar(x)
>
end foo
>
>
on bar(x)
>
return i
>
end bar
There is a difference between declaring a global at the top level of a
script and declaring it within an explicit run handler. In the former
case, the scope of the global is everywhere in the script. In the
latter, in order for another handler to reference the global it must be
declared in that handler as well.