Page 1 of 1
Weird behavior relating to scope and local subroutines
Posted: Tue Aug 05, 2025 8:27 am
by paues
I'm struggling to make sense of how scope works in local subroutines. I get an error that I don't understand. I have two files: parent.prg and child.prg.
parent.prg
Code: Select all
subroutine parent1
%path = @linepath + "child.prg"
exec %path
endsub
subroutine local parent2
%path = @linepath + "child.prg"
exec %path
endsub
wfcreate u 1 1
call parent1 ' Calls a subroutine
call parent2 ' Calls a local subroutine
child.prg
Code: Select all
subroutine local child1(string my_string)
my_string = "B"
endsub
string my_string = "A"
call child1(my_string)
@uiprompt(@wlookup("my_string", "string"))
delete my_string
The call to parent1 works just fine and results in a dialog box reading "MY_STRING". But the call to parent2 first results in an empty dialog box and then throws an error reading 'MY_STRING is not defined in "DELETE MY_STRING" in CHILD.PRG on line 8.' Is this the intended behavior?
Re: Weird behavior relating to scope and local subroutines
Posted: Tue Aug 05, 2025 3:59 pm
by EViews Gareth
Pretty much. If you run in debug mode and step through, you can see what happens. Local parent calls child, which means that child is local, which means that no created variables have workfile scope.
Re: Weird behavior relating to scope and local subroutines
Posted: Tue Aug 05, 2025 11:06 pm
by paues
In debug mode, it's pretty clear. But still rather confusing :-) If I'm understanding things correctly, objects in local subroutines work differently from objects outside local subroutines and never appear in a workfile at all. And that is why they aren't listed when using @wlookup. As a program can be called from within a local subroutine, this essentially means that I can never trust @wlookup.
Re: Weird behavior relating to scope and local subroutines
Posted: Wed Aug 06, 2025 12:50 am
by paues
I stumbled onto another issue, even more confusing than the last. This time there are three files: parent.prg, child_without_sub.prg, and child_with_sub.prg.
parent.prg
Code: Select all
subroutine local exec_child_without_sub
%path = @linepath + "child_without_sub.prg"
exec %path
!B = A
endsub
subroutine local exec_child_with_sub
%path = @linepath + "child_with_sub.prg"
exec %path
!B = A
endsub
wfcreate u 1 1
call exec_child_without_sub
delete(noerr) A
@uiprompt("child without sub done")
call exec_child_with_sub
delete(noerr) A
@uiprompt("exec_child_with_sub done")
child_without_sub.prg
child_with_sub.prg
Code: Select all
subroutine child_sub
endsub
scalar A
call child_sub
The call to the subroutine exec_child_without_sub works just fine, but the call to the subroutine exec_child_with_sub throws an error: A is not defined in "!B = A" in PARENT.PRG on line 10. What is happening here? The subroutine child_sub in child_with_sub.prg doesn't even do anything with the scalar A. The error persists even if I make child_sub a local subroutine that doesn’t even have access to A.
Re: Weird behavior relating to scope and local subroutines
Posted: Wed Aug 06, 2025 7:37 am
by EViews Gareth
The child subroutine inherits the "local" of the calling parent, and thus variables created inside the child subroutine are not available outside of that subroutine, even to the parent.
Re: Weird behavior relating to scope and local subroutines
Posted: Wed Aug 06, 2025 7:41 am
by paues
But the call to exec_child_without_sub works just fine even though the parent is a local subroutine. Should really the mere existence of a subroutine call in child_with_sub.prg change the scope of A?
Re: Weird behavior relating to scope and local subroutines
Posted: Thu Aug 07, 2025 12:58 am
by paues
Gareth, I presume that the "child subroutine" that you refer to is the subroutine child_sub in child_with_sub.prg and that the "calling parent" is either
- child_with_sub.prg itself or
- the local subroutine exec_child_with_sub in parent.prg.
In either case, I don't understand how your explanation relates to the setup that I specified in
my post: No variables are created inside the subroutine child_sub.
And if it simply was the call structure ("local subroutine executes program") that was the problem, then it is odd that the scalar A is available after the exec command in exec_child_without_sub but not in exec_child_with_sub. The only difference between the programs that those two subroutines calls (i.e., child_without_sub.prg and child_with_sub.prg) is a call to a subroutine that doesn't do anything. It is almost as if there was a limit of two (2) in "scope depth".
Re: Weird behavior relating to scope and local subroutines
Posted: Thu Aug 07, 2025 7:48 am
by EViews Gareth
No, you're right, that is weird.
As an aside, I tend not to use local subroutines at all - they can be very frustrating to work with.
Re: Weird behavior relating to scope and local subroutines
Posted: Thu Aug 07, 2025 9:23 am
by paues
Can we consider this my bug report? Or should I submit something more formally?
Re: Weird behavior relating to scope and local subroutines
Posted: Thu Aug 07, 2025 10:10 am
by EViews Gareth
Consider it reported.
Re: Weird behavior relating to scope and local subroutines
Posted: Fri Aug 08, 2025 2:29 am
by paues
This issue seems related. Again, I have three files: parent.prg, alert.prg, and child.prg. Running parent.prg generates a stream of dialog boxes in two batches.
The first batch calling a_caller_sub is straightforward: The scalar a is always observable and the scalar b is only observable when child.prg calls a_child_sub and not when it calls a_local_child_sub.
But in the second batch calling a_local_caller_sub, the first case is behaving strangely: Now, a_child_sub
can't observe the scalar a but it
can observe the scalar b.
(Admittedly, this example could have been streamlined

)
Re: Weird behavior relating to scope and local subroutines
Posted: Fri Sep 26, 2025 1:36 am
by paues
Have you made any progress with these bugs?
Re: Weird behavior relating to scope and local subroutines
Posted: Fri Sep 26, 2025 8:12 am
by EViews Gareth
No. It will be some time before they are addressed.
Re: Weird behavior relating to scope and local subroutines
Posted: Tue Feb 24, 2026 12:36 am
by paues
Any progress?
Re: Weird behavior relating to scope and local subroutines
Posted: Tue Feb 24, 2026 8:17 am
by EViews Gareth
Not yet :/