Page 1 of 1

Permanent variable not always recognized as such

Posted: 07 Jan 2013 14:57
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?

Re: Permanent variable not always recognized as such

Posted: 07 Jan 2013 16:59
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.

Re: Permanent variable not always recognized as such

Posted: 07 Jan 2013 18:06
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?

Re: Permanent variable not always recognized as such

Posted: 07 Jan 2013 20:09
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

Re: Permanent variable not always recognized as such

Posted: 07 Jan 2013 21:33
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?

Re: Permanent variable not always recognized as such

Posted: 08 Jan 2013 09:13
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.

Re: Permanent variable not always recognized as such

Posted: 08 Jan 2013 10:17
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;

Re: Permanent variable not always recognized as such

Posted: 08 Jan 2013 13:09
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!

Re: Permanent variable not always recognized as such

Posted: 08 Jan 2013 23:03
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.)

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 10:04
by admin
I'm warming up to the idea. Give me another night. :)

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 12:01
by PeterH
admin wrote:I'm warming up to the idea. Give me another night. :)
Thanks for the info :D

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 12:02
by admin
Aaah, I just woke up and yes, I'll do it. :)

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 12:08
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:

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 12:33
by admin
Yep. :)

Re: Permanent variable not always recognized as such

Posted: 09 Jan 2013 16:13
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