admin wrote:jacky wrote:
... I think global vars should only last for as long as the (current) script batch execution goes on...
Otherwise, we'd then be talking about "session variables", not global variables...
Same here!
A question: what's the use of global variables?
First something else: how to use subroutines and functions? I assume to use them, you need parameters and return-values. So that SRs + functions "can" use values of variables in calling scripts, and calling scripts after return can use values "calculated" in deeper levels.
Eventually you can return values by parameters to calling scripts. (A bit dangerous...)
I think, if this is possible there is not very much use for global variables on the "currently executing" scipts? (Not "no use", but not so very much.) But globals should
not be used for substitution of parameter-usage
On the other side I imagine a sequence like
- select a directory (for destination), call a script to remember it and to initialize some global vars
- multiple: search and select some files (maybe pictures) and call another script to remember (accumulate)
- when ready, after all, call a 3rd script to make final actions with all this data
Or setting a global $debug to 0 or 1, to test it in many scripts to decide if msg-commands for debugging should be made: for switching on / off debugging you must not change your scripts, but only the contents of a global variable.
(Better: use between 0 and 5, and test:
If $debug > 3 then msg ... to have different debug levels, 5 to debug all, 0 for nothing.)
For this I would need global vars spanning several scripts. (In other words: "last for life of XY".) For such things I see more use than for globals just in "current executing" scripts.
And: for
both you have the problem of unique names: even if you use a subroutine from someone else inside your scripts, possible globals must be unique. So using global vars always forces use of conventions between all people for unique names (you will never get) or adapting names of global vars to local conventions for all "imported" scripts.
Another thing: in any script, I'd expect, a variable will be used as global only
after being defined so. In this case a variable-name must only be unique in those scripts defining it as global - others would use a local value.