Re: Modifications and Variables
Re: Modifications and Variables
- Subject: Re: Modifications and Variables
- From: Axel Luttgens <email@hidden>
- Date: Sat, 04 Jan 2003 20:26:57 +0100
(sorry, I again got trapped and sent this to the original poster...)
Paul Berkowitz wrote:
>[snip]
>
>So it seems that every undeclared variable in the run handler, not just
>declared globals (which I knew about) and properties, is persistent,
even if
>its scope is local to the run handler.
>
This is consistent with what I always thought to have understood while
reading the ASLG and its predecessors (yes, this is a rather convoluted
sentence, but one never knows with that kind of reading ;-) )
Allow me to somewhat develop what I mean, even if it may seem too obvious.
But first of all, a little reminder for those who don't remember such
things precisely:
A script's implicit run handler (IRH) consists of all statements at the
top level that are not property declarations, script object definitions
or command handlers.
A script's IRH can be converted into an explicit run handler (ERH) by
grouping all IRH's statements and surrounding them by 'on run... end'.
A script may have an IRH or an ERH, but not both.
So, in following script:
-- Script1
set G to 100
on test()
global G
return G
end test
test()
--> 100
the IRH consists of statements 'set G to 1' and 'test()', in that order.
These are the statements to be executed when the script receives a run
event.
Statement 'set G to 100' implicitely declares variable G and set its
value to 100; but the interesting part is that G is in fact declared as
a global variable. This is exactly as if one had written:
-- Script2
global G
set G to 100
on test()
global G
return G
end test
test()
--> 100
If one wants to restrict the scope of G to the IRH only, that variable
must explicitely be declared as a local variable:
-- Script3
local G
set G to 100
on test()
global G
return G
--> variable G is undefined
end test
test()
Note that in the above, there is still a global variable named G that
may be referenced from within any handler by using "global G".
OK. Let's now convert above scripts so that they use ERHs:
-- Script1bis
on run
set G to 100
test()
end
on test()
global G
return G
end test
--> 100
-- Script2bis
on run
global G
set G to 100
test()
end
on test()
global G
return G
end test
--> 100
-- Script3bis
on run
local G
set G to 100
test()
end
on test()
global G
return G
--> variable G is undefined
end test
With above examples in mind, I may now briefly summarize my
understanding of the ASLG about those matters (especially pp. 303-304
and 311-316):
A variable declaration is the first valid occurence
of a variable identifier in a script.
Any variable declared at the top level of a script
is a global variable, unless explicitely declared
through a "local" statement.
The top level of a script must be understood as
including every statement that should appear in an
explicit run handler.
And I should add (a bit provokingly?) that an explicit run handler
appears to me more as a cosmetic matter than anything else; there is no
such handler, there are just top level statements.
>(What put me on to this is the fact
>that when you open a script in Script Debugger you can read all the values
>of every top-level variable from its last run.) How odd. What's it for?
>
I guess the idea was that if one declares a variable at the top level of
a script, it is because it must have a global meaning for the script;
otherwise, why should a variable be declared at the top level?
The real source of confusion resides in the possibility to write an
explicit run handler, which does not behave as regular handlers (for
scoping matters).
>
>Does anyone know for certain if this sort of saving of new values at
the end
>of a script run is indeed something that would create an error if the
script
>was run by a user with execute but not write privileges? Naturally, I've
>declared all the variables as local in the meantime, but I'm interested to
>know if anyone has first-hand experience?
>
>
Good question.
I think remember that this could be the source of error messages.
I thus immediately tried, by saving this script as an application (Mac
0S 10.2.3):
-- Script4
try
set G to G + 100
on error
set G to 100
end try
on test()
global G
display dialog G
end test
test()
The file's authorization were thus:
-rwxr-xr-x luttgens staff
Running that application several times (by double clicking its icon)
displayed, as expected, 100, 200, 300... (and the modification date
changed too).
Changing the owner to root hindered the saving of the value of variable
G, so that each run showed, say, 400, 400, 400...
But no error messages were issued: the value of G was just silently not
saved.
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.