Page 1 of 2

How to execute a command?

Posted: 18 Jan 2021 21:14
by MBaas
I have a scenario in a script where I process a file and end up with $cmd containing a command the user wants to execute. That command can be a simple as $foo=1+2; or I might load another script that assigns a value $foo somehow.

I can't find a way to deal with that correctly - despite attempts to read up on "Interpolation", searching the "Scripting Commands" for various keyswords that came to mind etc.

A simple repro is this:

Code: Select all

  
  global $foo;
  $foo = "";
  $cmd = '$foo=1+2';  // this comes from file normally...
  load $cmd,,"s";      // how to execute what's in $cmd???
  text $foo;  


How can I get it to execute this - it seems I'm unable to find the appropriate command?

Re: How to execute a command?

Posted: 18 Jan 2021 22:00
by highend
eval()

But
01. It may need correct quoting of what to eval
02. You need to assign it to a real variable, '$foo=<expression>' wouldn't work

Re: How to execute a command?

Posted: 19 Jan 2021 04:32
by jupe
If you are just asking how to make that script work as I think you intend, then this should work.

Code: Select all

  global $foo;
  $foo = "";
  $cmd = 'global $foo; $foo=1+2';  // this comes from file normally...
  load $cmd,,"s";      // how to execute what's in $cmd???
  text $foo;  

Re: How to execute a command?

Posted: 19 Jan 2021 07:14
by MBaas
Thanks jupe! I thought the "global" in the parent made the var global once and for all, but if a child wants to act on that var, it also needs to globalize it, right? That wasn't obvious after RTFMing.

But still, something doesn't work quite as I expect - it just doesn't "keep" the value. (Unfortunately the tracer does not show globals - is there perhaps a tweak to achieve that?).
I think the screenshot of my actual in the tracer shows a use-case that corresponds to the repro I gave - but I must be having a dumb moment, because, as you see, the var $MyMenu is empty after executing it.
19-01-_2021_07-05-36.png
19-01-_2021_07-05-36.png (17.54 KiB) Viewed 2292 times

Re: How to execute a command?

Posted: 19 Jan 2021 08:09
by jupe
Yes globals need defining in each script.

I am a little unclear again what you are asking, but you are defining $MyMenu in the child script, but expecting the variable response in the main script within which you just unset the variable, so the resolution should be the same as last time but the inverse, the var needs to be globalized in the main script, did you do that? I can't tell from that screenshot, but globals do show up in the "tracer" if they are defined properly, no tweak necessary.

Re: How to execute a command?

Posted: 19 Jan 2021 08:45
by MBaas
Well, I have a script that should execute user-defined code. I don't know if there are "best practices" to do that, but I figured I'd have a global (first line in the script is "global $MyMenu;" actually) and the user-code could manipulate that. Since it happend in a loop, I thought it would be best to unset it in every loop and check if it was set after executing user-code.

If the "global"-command in my script is all that was required to properly set it up, I am rightfully expecting to see it in the "stepper" (since you quoted my word "tracer", what is proper XY-lingo for that tool?), right?
Not happening here :(
19-01-_2021_08-41-42.png
19-01-_2021_08-41-42.png (14.89 KiB) Viewed 2279 times

Re: How to execute a command?

Posted: 19 Jan 2021 09:04
by jupe
If you unset a variable, even if it is subsequently made global in a child script, it is still kept unset in the main script. Just based on your screenshot, this is a quick mock up that I think should do what you are trying to do.

Code: Select all

	global $MyMenu;
	$cmd = 'global $MyMenu = <get list_recentlocations>;';
	load $cmd, , "s";
	text "Executed " . $cmd . <crlf> . 'menu=' . $MyMenu;
I quoted tracer because I had never heard that term and wasn't sure if you meant the variables or the step dialog., I wasn't indicating your term was in any way wrong, personally I call it the stepping dialog, and the offshoot the variables dialog, but I am not to say which is right or wrong.

Re: How to execute a command?

Posted: 19 Jan 2021 14:45
by MBaas
Wow, thanks! It was really as simple as replacing the unset with $MyMenu="";
It was "just" that stupid misunderstanding - and the hlp even says that the var will be "destroyed". It's understandeable that it wouldn't be global any more when it gets back to live, but I just did not see that before. :oops:

Re: How to execute a command?

Posted: 19 Jan 2021 14:48
by klownboy
Many times when dealing with variables and menus it's best to use a perm variable especially if you need the variable in a menu title. Just make sure you unset the perm when the script terminates. Again I'm not sure what you are after in the end for output, but something like this works:

Code: Select all

	perm $MyMenu = "My Recent Locations Menu";
	$cmd = $MyMenu<crlf><get list_recentlocations>;
	load $cmd, , "s";
	text "Executed " . $cmd . <crlf> . 'menu=' . $MyMenu; 
	unset $MyMenu;

Re: How to execute a command?

Posted: 19 Jan 2021 16:24
by MBaas
To be honest, the word "permanent" sounded frightening when I RTFM'ed while studying other code-samples. The least I would want to do is leave something permanent in the environment! But maybe that's a too traditional viewpoint inspired by frameworks and conditions which do not apply here.

But, as I learned (the hard way), as "unset" can erase the var, it's not as "dangerous" as I thought :wink: But ofc, determining the appropriate context for its use is something that overchallenges me. We'll see - I will definitely post the final result (next year, when I learned and asked enough to get it working...🤣)

Thanks!

Michael

Re: How to execute a command?

Posted: 19 Jan 2021 16:47
by klownboy
Yes honestly I try to shy away from using any permanent variable in situations where they are carried over and left permanent in the system after the script is done, but using them within a script and clearing them out when it exits, that 's no problem for me. Sometimes it's best to use the special hidden sub "_Terminate"; in the script. It's always run when the script ends (i.e., it doesn't have to be called).
So in this case...

Code: Select all

"_Terminate";
   unset $MyMenu;

Re: How to execute a command?

Posted: 19 Jan 2021 17:45
by MBaas
Ah, sweet! 8)

Re: How to execute a command?

Posted: 01 Feb 2021 09:51
by MBaas
So I have adopted the approach to execute $cmd via load $cmd,,s; and while it works nicely in most cases, I now came across one example where it didn't.
Repro:

Code: Select all

$cmd="#117;";load $cmd,,s;
This should copy the active file content to the clipboard - but it doesn't. (I had selected/highlighted/focussed a file and pasted that code into the address bar for the easiest repro I could imagine - but didn't get the filecontent to the clipboard)
Probably I am missing something terribly simple - but I don't see what it might be :cry:
Would appreciate some insight :wink:

Re: How to execute a command?

Posted: 01 Feb 2021 09:59
by highend
Does #117; from the address bar work?

Re: How to execute a command?

Posted: 01 Feb 2021 10:02
by MBaas
Yes, it does.