How to share variables with Yab lib?
#1
The application I am writing is going to need to contain 15+ subroutines, all of which will be 500+ lines of code. I decided to just make each subroutine its own .yab file and then I will IMPORT it at the beginning of my main program.

I've completed 1 subroutine, saved it as 'routineExample.yab' and then used "EXPORT SUB routineEXAMPLE()" and then in my main program I call it with "IMPORT routineExample" however, the imported subroutine isn't sharing variables I've declared in my main program & which I rely on in routineEXAMPLE().

Is it possible to do this?

Also, curiously, when I import the subroutine file, I cannot used "#! /bin/yab" at the beginning of the imported .yab file or I get an error.
Reply
#2
(10-07-2024, 07:42 PM)PurpleStudio Wrote: Also, curiously, when I import the subroutine file, I cannot used "#! /bin/yab" at the beginning of the imported .yab file or I get an error.

That is normal. When you IMPORT a library it actually takes the text of the library and pastes it onto the end of your main program, so now you have that shebang line in the middle of your program, which is ... not good.
Reply
#3
(10-07-2024, 07:42 PM)PurpleStudio Wrote: I've completed 1 subroutine, saved it as 'routineExample.yab' and then used "EXPORT SUB routineEXAMPLE()" and then in my main program I call it with "IMPORT routineExample" however, the imported subroutine isn't sharing variables I've declared in my main program & which I rely on in routineEXAMPLE().

Is it possible to do this?

Your global variables should definitely be available. Double-check that you've used them in the same case. Variables are case-sensitive.

local variables need to be passed to the library the same way you'd pass them between subroutines.
Reply
#4
(10-08-2024, 01:18 AM)clasqm Wrote:
(10-07-2024, 07:42 PM)PurpleStudio Wrote: I've completed 1 subroutine, saved it as 'routineExample.yab' and then used "EXPORT SUB routineEXAMPLE()" and then in my main program I call it with "IMPORT routineExample" however, the imported subroutine isn't sharing variables I've declared in my main program & which I rely on in routineEXAMPLE().

Is it possible to do this?

Your global variables should definitely be available. Double-check that you've used them in the same case. Variables are case-sensitive.

local variables need to be passed to the library the same way you'd pass them between subroutines.

At the moment, none of the variables in my main program are declared 'local' and from YAB Documentation, unless explicitly stated, all variables are declared global.

I checked my code again. variables are character-for-character, taking case sensitivity into account. Still nothing. I tried just pasting the subroutine from the file I want to import directly in my main program, and it works just fine, but as soon as I remove it and use the import file$ it stops working.

I wonder if it is because of WHERE I am importing the file. Right now, it's at the very top of my main program below #! /bin/yab. I wonder if I paste that import at the bottom after all variables are declared, it would work...

I really don't want to have to pass variables via subroutine arguments (example: routineExample(num1, num2, string1$, string2$)) because then I can only "RETURN" one value and I would have several things to return (such as a msg$).
Reply
#5
if you plan on building your code, you will need to explicitly pass the variables. The following is an xample passing multiple variables to/from an imported library:

#!yab
x=1
y=2
z=3
import test
print x,y,z
dim veriables(3)
veriables(1)=x
veriables(2)=y
veriables(3)=z
dim v$(3)
versible$=test$(veriables())
//print versible$
num=token (versible$,v$(),"|")

x=val(v$(1))
y=val(v$(2))
z=val(v$(3))

print x,y,z
end

test.yab:


export sub test$(v())
x=v(1)
y=v(2)
z=v(3)
x=x+1
y=y+1
z=z+1

print x,y,z


return str$(x)+"|"+str$(y)+"|"+str$(z)

end sub

note: the import command refers to the file containing the subroutine. in this case test.yab ( the .yab is understood and is not used in the import statement) test.yab must be in the program directory or in /boot/home/config/settings/yab/
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)
Free Web Hosting