Execute statement and local variables

I use the execute statement regularly. Sometimes I have difficulty getting the syntax correct, but this very simple example had me stymied until a minute ago.

The following code works as expected:

let lv2022="Hello World"
alertsheet lv2022

But this generates an error:

let myprogram={let lv}+datepattern(today(),“YYYY”)+{=“Hello World”}
execute myprogram
alertsheet lv2022

The error is “field or variable [lv2022] does not exist”

If you change myprogram so it creates a fileglobal variable, everything works. Apparently, local variables created in an execute program cease to exist once that program is finished, even though execute is part of a larger program. In the legal world, we would call this type of problem a trap for the unwary.

Simple fix for this situation:

The same would be true if you called another procedure as a subroutine. The subroutine creates a local variable. When it ends, and returns to the main procedure, the local variable no longer exists. An execute is a call to a dynamically created subroutine. Local variables created by the subroutine exist, only within the subroutine.

What you could do is this.

let myprogram={usecallerslocalvariables
let lv}+datepattern(today(),"YYYY")+{="Hello World"}
execute myprogram
alertsheet lv2022

That is what the ExecuteLocal custom statement does.

Correction: ExecuteLocal was a custom statement in Panorama 6, but in Panorama X, it’s a regular statement.

Thanks. I am glad to learn there is such a simple solution.

This conceptualization on your part is causing the “trap” for you. The code in the execute is NOT part of the larger procedure. It’s a separate procedure, just as if you had used the call statement. True, the text of the code in the execute is embedded in the larger procedure, but what is actually happening is that the execute statement takes that text and constructs a temporary procedure out of it, and then calls that procedure. In fact, at any point in the execute code you can put a return statement in and Panorama will skip out of the execute code and on to the next statement after the execute.

Indeed, the problem was my understanding of the treatment of local variables in an execute statement. Now I know better.