Re: global variable not being recognized by script object su
Re: global variable not being recognized by script object su
- Subject: Re: global variable not being recognized by script object su
- From: email@hidden
- Date: Wed, 28 Feb 2001 01:49:18 -0500
On Tue, 27 Feb 2001 12:16:20 -0600, Bill Planey <email@hidden> asked,
>
I am having problems with variables I am setting as globals.
>
They seem not to be understood by the script files that load
>
(which refer to the global variables after they have been set).
>
>
For example, I have a script that starts "on run..."
>
>
and it sets a number of global variables before doing anything
>
else. Here is one:
>
>
global AnyDesktop
>
set AnyDesktop to path to desktop folder as string
>
>
-- opens the file where many global variables for file
>
-- and directory locations are set
>
set GlobalsFile to scriptFolder & "GlobalsHandler" as string
>
set GlobalsObject to (load script file GlobalsFile)
>
>
tell GlobalsObject
>
DoSubroutine()
>
end tell
>
>
... To test, I have a display dialog statment as the first line
>
of the DoSubroutine handler:
>
>
display dialog AnyDesktop
>
>
>
The script breaks with the following message:
>
>
"The variable AnyDesktop is not defined."
The scope of a variable is determined when the script is compiled. The first
time the compiler sees the variable, it determines what type of variable it is.
In a separately compiled script that is later loaded with "load script", it
compiler doesn't have any clue that "AnyDesktop" is a global, and so doesn't
create a global variable; it creates a local variable.
This isolation is a good thing. You don't want someone messing up what a script
does by accidentally using a name that is the same as a variable inside the
script. Only if the script says, "global x" will it allow the global definition
of "x" in.
I've built up a large library of scripts and handlers that I have tested and
reused, and I've gained enormous confidence that the do exactly what they are
supposed to. And I can feel that confident because what each handler or object
does is dependent only on the explicit arguments that I pass in to it, the
effect it has is only on the result returned, and (for a script object) on the
internal state of the script object. Nothing is reaching out and checking the
weather, or the phase of the moon, or the value of some global variable that I
may or may not have remembered to define. And the handlers and script objects
are definitely not reaching out and fiddling with other parts of the script
behind my back.
And I don't need to use funny Microsoft names (like "gStrMytext") to protect
myself against accidental bumping together of names in the global name space,
because there's nothing to bump.
So, I recommend you seriously reexamine the need to use global variables. They
are nothing but trouble, causing invisible connections between pieces of your
program. If a script or handler uses a piece of information, you should pass it
in explicitly as an argument, or out as the result, or access it as a named
property of a script object.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden