Re: Scope of globals, parents and children, "load script" and all that jazz
Re: Scope of globals, parents and children, "load script" and all that jazz
- Subject: Re: Scope of globals, parents and children, "load script" and all that jazz
- From: Sander Tekelenburg <email@hidden>
- Date: Sun, 20 May 2001 04:32:42 +0200
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
At 20:42 +1000 UTC, on 17/05/2001, Timothy Bates wrote:
>
On 17/5/01 1:21 PM, "Sander Tekelenburg" <email@hidden> wrote:
>
> I separated some stuff into a main script (parent) and a
>
> separate script library (child) that is called by the main script with the
>
> "load script" command.
[...]
>
>why did I need to:
>
> 1) declare those same globals again, at the top of the script
>
> 2) declare them yet again within each handler! (well, only those globals
>
> that apply to that particluar handler)
>
>
First off, you don't need the original declarations any more: the data are
>
stored in the object, so having them in the main handler is redundant.
When I delete those lines that declare the globals to be globals from the
main script, the script tells me that some variables are not defined. Perhaps
I didn't explain myself right. I did not mean I need to define the values of
those globals. I mean that I (for a reason I still don't understand) need to
define those variables as globals again in the child.
I would post an example to better explain what I'm doing, but I have too much
trouble understanding the issue... :)
>
If their values are needed in the main object
They are.
>
, access them through the object
>
pPropertyName of oObject, or Object's pPropertyName
Sorry. I just don't understand what you mean. I know properties to be
variables whose value is stored within the script. Apparently there's more to
them?
>
Second off, if they are properties, they will be available within the
>
handlers of your object. You don't need to declare them again, vis
>
>
if your object contained these lines:
>
>
property pVar1:"hello Teklenburg"
>
>
to sayHi()
>
return pVar1
>
end
>
>
then
>
>
tell object to sayHi()
>
--> "hello Teklenburg"
The variable "object" is not defined. When I define it, like this:
property pVar1 : "hello Tekelenburg"
script object
to sayHi()
return pVar1
end sayHi
end script
tell object to sayHi()
- --> "hello Tekelenburg"
your script works. But I seem to be missing something (that's probably
obvious :)) here: how does this apply to loading a compiled script?
>
>I thought the loaded script would 'see' the
>
> Parent's globals
>
absolutely not. that would be a disaster. think about what happens if an
>
object reuses the name of a var in your script without your knowing ...
How would a variable name ever be used without my knowing? I am the author. I
decide how to name my variables. I make it a point to never use the same name
for a variable within 1 script.
Am I missing a key issue?
>
think "objects are like people, I find out what they believe by asking them
>
questions, not by reading their minds"
But doesn't the very fact that globals exist mean that objects are _not_ like
people? :)
To stay with your analogy: Up until now I've considered globals to be
something like public knowledge, available to and known by all 'people'. It
certainly worked like that in every all-in-one script I wrote so far.
>
> apparently both the parent and the child must be 'manually'
>
> informed of each other's globals. Why? And is there a more elegant way to
>
> do this?
>
the key here is to think "if I have to tell the object about this variable,
>
maybe it should be a property of that object, and not of my script?"
Are you saying I should get rid of globals altogether?
In general I try to not use globals too easily. But in this script, some
things are referred to over and over again; both in several handlers and
within the main script. I doubt I can get rid of all globals _and_ keep the
whole thing comprehensible...
TIA
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <
http://www.pgp.com>
iQA/AwUBOwcsx+sywKfXgqKdEQKDxwCg6cyVk1AkRRIi6bIm4NP1KypN5m0AnReF
a/6JzBjxbqmUziFmODr7CV/D
=Pfoi
-----END PGP SIGNATURE-----
--
Sander Tekelenburg, <
http://www.euronet.nl/~tekelenb/>
Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"