Weird behavior relating to scope and local subroutines

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Weird behavior relating to scope and local subroutines

Postby paues » Tue Aug 05, 2025 8:27 am

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?
Last edited by paues on Tue Aug 05, 2025 10:51 pm, edited 1 time in total.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Tue Aug 05, 2025 3:59 pm

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.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Tue Aug 05, 2025 11:06 pm

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.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Wed Aug 06, 2025 12:50 am

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

Code: Select all

scalar A

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.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Wed Aug 06, 2025 7:37 am

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.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Wed Aug 06, 2025 7:41 am

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?

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Thu Aug 07, 2025 12:58 am

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
  1. child_with_sub.prg itself or
  2. 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".

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Thu Aug 07, 2025 7:48 am

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.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Thu Aug 07, 2025 9:23 am

Can we consider this my bug report? Or should I submit something more formally?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Thu Aug 07, 2025 10:10 am

Consider it reported.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Fri Aug 08, 2025 2:29 am

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 :roll: )
Attachments
alert.prg
(213 Bytes) Downloaded 424 times
parent.prg
(373 Bytes) Downloaded 385 times
child.prg
(247 Bytes) Downloaded 377 times

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Fri Sep 26, 2025 1:36 am

Have you made any progress with these bugs?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Fri Sep 26, 2025 8:12 am

No. It will be some time before they are addressed.

paues
Posts: 201
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Weird behavior relating to scope and local subroutines

Postby paues » Tue Feb 24, 2026 12:36 am

Any progress?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13583
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weird behavior relating to scope and local subroutines

Postby EViews Gareth » Tue Feb 24, 2026 8:17 am

Not yet :/


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests