Permanent variable not always recognized as such

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Permanent variable not always recognized as such

Post by PeterH »

A Strange thing again :shock:
(This time my usage might be strange? :whistle: )

Basic: after a variable has once been defined as permanent, it will be used as permanent everywhere, without any further definition.

My problem:
- I start a script a.xys
- from a.xys I load script b.xys
- in b.xys I define some permanent variables and assign values (see it as kind of global constants)
- b.xys is ended, a.xys continues
- a.xys references a variable just defined as permanent - but still uses a (non-initialized) local variable

No question: if I start the same again the perm is already defined, and everything works as expected.

That's the problem:
- formally I think it's wrong as it is now (variables are defined perm, but "not always" recognized as such)
- I wouldn't love to define all the permanent variables *everywhere* where I might possibly use them for the very first time.

Any chance that this could be changed?

Borut
Posts: 1472
Joined: 19 Oct 2010 19:29
Location: Win10 Pro 64b 22H2, @120DPI (125%)

Re: Permanent variable not always recognized as such

Post by Borut »

Not sure any more (was long time ago), but it could be that that was the reason I was using the following two commands construct (is just an example) in all procedures, when defining perms:

Code: Select all

 perm $p_TabsetBase; $p_TabsetBase = ($p_TabsetBase=="") ? '<xydata>\Panes' : "$p_TabsetBase";
But, it can very well be that this does not have anything to do with your question/suggestion.
Win 10 Pro 64bit

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

Re: Permanent variable not always recognized as such

Post by admin »

PeterH wrote: My problem:
- I start a script a.xys
- from a.xys I load script b.xys
- in b.xys I define some permanent variables and assign values (see it as kind of global constants)
- b.xys is ended, a.xys continues
- a.xys references a variable just defined as permanent - but still uses a (non-initialized) local variable

No question: if I start the same again the perm is already defined, and everything works as expected.

That's the problem:
- formally I think it's wrong as it is now (variables are defined perm, but "not always" recognized as such)
- I wouldn't love to define all the permanent variables *everywhere* where I might possibly use them for the very first time.

Any chance that this could be changed?
It works like this: When a.xys is loaded into memory any permanent variables are scanned. Permanent variables added within the scope of a.sys are added on the fly. But perms defined in b.xys are not visible in a.xys unless a.xys is reloaded.

I could change this but I'm not sure if I should. A subordinated procedure should not change variables in the calling procedure, or should it?

Borut
Posts: 1472
Joined: 19 Oct 2010 19:29
Location: Win10 Pro 64b 22H2, @120DPI (125%)

Re: Permanent variable not always recognized as such

Post by Borut »

admin wrote:I could change this but I'm not sure if I should. A subordinated procedure should not change variables in the calling procedure, or should it?
I feel it should (as PeterH does, I believe), since this all brings memories of FORTRAN COMMON. I found this example googling a bit:

http://www.mathcs.emory.edu/~cheung/Cou ... oping.html
Win 10 Pro 64bit

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

Re: Permanent variable not always recognized as such

Post by PeterH »

admin wrote:It works like this: When a.xys is loaded into memory any permanent variables are scanned. Permanent variables added within the scope of a.sys are added on the fly. But perms defined in b.xys are not visible in a.xys unless a.xys is reloaded.

I could change this but I'm not sure if I should. A subordinated procedure should not change variables in the calling procedure, or should it?
I expected it to work now just as you explained.

But I think it should not!

1) The official rule (as I understand) is that if a variable is permanent, then it is known everywhere.
2) (for my situation) the subordinate procedure doesn't "change" the variable for the calling proc, as it doesn't exist there yet - it "only" defines it (as perm)
3) consistency: in the moment, when the main routine is called the first time, this variable now is used local - for every followup call it's perm.

"Changing" the variable (one) level(s) above is the rest-risk of the perm being universally known - so it also can be universally changed.
But it's the same inside a script when a global or perm is defined: it overwrites a currently used local of the same name.

So, from side of the logic, I think it should be changed. And If I understand you correct, it's not too complicate to do?

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

Re: Permanent variable not always recognized as such

Post by admin »

This is very difficult to decide (and I'm into totally other areas at the moment), and a change here would potentially affect old code. I don't think this is a clear bug that just has to be fixed.

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

Re: Permanent variable not always recognized as such

Post by admin »

Experimentally I added an undocumented command refreshperms. Play with it:

Code: Select all

// usage of refreshperms
  unset $foo; // ensure $foo is gone
  $a = "A"; // some local var
  // this script will set the perm $foo: perm $foo; $foo = "hi!";
  load "setpermfoo.xys";
  refreshperms;
  echo $foo;

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

Re: Permanent variable not always recognized as such

Post by PeterH »

admin wrote:This is very difficult to decide (and I'm into totally other areas at the moment), and a change here would potentially affect old code. I don't think this is a clear bug that just has to be fixed.
I think yesterday I was too close to the problem...
...today I think have a much better way to decide!

Either the script is designed to use a permanent variable - then it's best the variable will be seen as permanent as soon as it is defined somewhere
Or the script is not designed this way - then it will fail for each but the very first call

The result, as I see it: it never (but maybe for 1 start) really helps if the perm is not immediately known to the outer script,
but it generally is OK if it's status is immediately known.

And even 1 more argument: to be recognized permanent variables should have unique names to be immediately distinguished - and it would not be helpful if, for some reason, they would be used as local.

So I'd expect no current existing script to be correct and to be avoided by this change.

If you would still not change: refreshperms could just be a circumvention to the problem (if it works as I expect).
Each time I call a script that could define a permanent variable being used in the current script I would have to execute this command...

edit: if anyone thinks he has a script having problems with this change: please give a note!

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

Re: Permanent variable not always recognized as such

Post by PeterH »

admin wrote:Experimentally I added an undocumented command refreshperms. Play with it:

Code: Select all

// usage of refreshperms
  unset $foo; // ensure $foo is gone
  $a = "A"; // some local var
  // this script will set the perm $foo: perm $foo; $foo = "hi!";
  load "setpermfoo.xys";
  refreshperms;
  echo $foo;
I've experimented with refreshperms - works as it should: after refreshperms the new defined perms of the sub-script are known to the rest of the outer script.

But I'm still convinced that it would be of no advantage if it's not happening automatically, while it can be of advantage if it does.

So you would lose nothing, but win something, if you change it.
(Only "something", as it only affects special situations.)

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

Re: Permanent variable not always recognized as such

Post by admin »

I'm warming up to the idea. Give me another night. :)

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

Re: Permanent variable not always recognized as such

Post by PeterH »

admin wrote:I'm warming up to the idea. Give me another night. :)
Thanks for the info :D

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

Re: Permanent variable not always recognized as such

Post by admin »

Aaah, I just woke up and yes, I'll do it. :)

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

Re: Permanent variable not always recognized as such

Post by PeterH »

admin wrote:Aaah, I just woke up and yes, I'll do it. :)
Still more thanks for this info :P

That would mean I can forget refreshperms again, I think! :biggrin:

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

Re: Permanent variable not always recognized as such

Post by admin »

Yep. :)

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

Re: Permanent variable not always recognized as such

Post by PeterH »

Code: Select all

v11.90.0202 - 2013-01-09 14:43
    * Scripting: From now on Permanent Variables created / modified in
      a called script are immediately visible / updated in the calling
      script when the called script returns. 
Looks pretty good :appl: :appl:

Have tested without "refreshperms;" :arrow: full success, as expected :P

Thanks a lot! :D

Post Reply