Initialize/Terminate variables scope

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Initialize/Terminate variables scope

Post by Marco »

admin wrote:OK, this will work now:
...
Awesome, thank you!
Btw, I'll answer to my question above: globals can't be used in captions because they have to be declared as such first. Makes sense.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Initialize/Terminate variables scope

Post by admin »

Yes.

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post by PeterH »

Marco wrote:
PeterH wrote:So if I define a global in scripts like "_Initialize", "_somename" or "Main" it should function alike. And the same is for assigning values.

From my POV _Initialize is nothing but a script that's automatically executed before label-selection is shown.
Exactly.
Imo, perm should be used for sharing data between different scripts, i.e. scripts coming/belonging to entirely different sources.
Thus, scripts belonging to a MSR should use globals, while scripts coming from different xys files (or different buttons, or catalog items) should rely on perms.
This would explain also my doubts about perms in script captions: shouldn't globals alone suffice?
I'd say no - hope I'm exactly right...

A Global lives from the time it is defined until the current script stack terminates - then it's automatically undefined: looses it's global status and it's value.
A Perm lives from the moment it's first defined (at least) until XY ends. (You *can* also save/restore them.)

This means: global is used to communicate between a group of nested scripts, while perm can communicate between (in sense of nesting) independent scripts.

But perm has one more advantage: as long as a variable is defined as perm it will be trated as such by all references in all future scripts - until undefined (or XY ends). So you have to define a var as perm only once.
This also means the risk of problems is a bit bigger on perm: if a variable is defined perm, and any totally independent script happens to use this name - it uses the perm variable. If a var is "only" defined global and a script uses the same name without defining it global, it will use a local variable and not affect the global.
But as Don said: you should use names for global and perm variables showing their nature - then you will not misuse them by chance.

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Initialize/Terminate variables scope

Post by klownboy »

Marco wrote:
admin wrote:OK, this will work now:
...
Awesome, thank you!
Btw, I'll answer to my question above: globals can't be used in captions because they have to be declared as such first. Makes sense.
Ah yes, that makes perfect sense. You can't see what isn't there yet. Thanks Marco, PeterH and thanks once again Don.
Ken

Post Reply