PureBasic Modules: A Quick Tutorial

Share your advanced PureBasic knowledge/code with the community.
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: PureBasic Modules: A Quick Tutorial

Post by DontTalkToMe »

Code: Select all

DeclareModule myModule
EndDeclareModule

Module myModule 
  Define myVar = 123
EndModule

Debug myModule::myVar   ; == 0 ?

However, you'd notice that the resulting output is still zero. Why is this?
The output can't be be zero since the code is not valid.
myVar is local to the module and it's not accessible from the external debug statement.

Less important, but I would add a .f to

Code: Select all

Global myVersionNumber = 1.23
since it's a float in the example after that which is a variation of this one.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 613
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: PureBasic Modules: A Quick Tutorial

Post by minimy »

Wauuuu master TI, thanks for this very comprensive tutorial!!!
As allways PBforum is the best!!
Great community with a lot of gray matter!!

I now can put, halt and catch fire like a module in my HAL9000 :mrgreen: :mrgreen: HAHAHA

THANKS AGAIN TI!!! +1
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
chikega
User
User
Posts: 40
Joined: Fri Dec 04, 2020 3:19 am

Re: PureBasic Modules: A Quick Tutorial

Post by chikega »

Very nice tutorial on Modules! It's been added to my PureBasic Evernote Notebook 8)
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
salutcava
User
User
Posts: 10
Joined: Fri May 03, 2013 10:50 am

Re: PureBasic Modules: A Quick Tutorial

Post by salutcava »

Hello,
Tanks for your quick & simple tutorial on the use of modules. I had read it mostly for the interoperability section (would like to call a procedure in the parent/main code from code in a module) for my needs. Since the use of a common/dummy module wasnt helping me (or couldnt figure out how and don't want my whole code to be contained in modules). I post here a "workaround" to call a procedure which is in the main code from a line of code contained inside a module :

Code: Select all

Procedure MainProcedure(arg.s)
	debug arg
EndProcedure

DeclareModule MyModule
	Prototype proto(argument.s)
	Global MainProc.proto=0
	;.
	;.
	;.
EndDeclareModule

Module MyModule
	Procedure ModuleProcedure(...)
		;.
		;.
		;.
		MainProc(text$)
		;.
		;.
		;.
	EndProcedure
EndModule

;.
;.
;.

MyModule::MainProc = @MainProcedure()

;.
;.
;.

End

Hope it will help. Bye !
User avatar
lgb-this
User
User
Posts: 32
Joined: Sat Aug 30, 2014 9:00 pm
Location: Switzerland
Contact:

Re: PureBasic Modules: A Quick Tutorial

Post by lgb-this »

salutcava wrote: Mon Mar 20, 2023 11:33 am Hope it will help. Bye !
Brilliant ! I was looking for this topic: "call an external procedure in a module" and your proposal worked !
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Re: PureBasic Modules: A Quick Tutorial

Post by KarLKoX »

Very nice tutorial ! 👍

There is a tiny typo error :
"The variable myPrivateVariable and the function myPrivateFunction() are both inaccessible to the parent program"

To be changed to :

The variable myPrivateVariable and the function myPrivateProcedure() are both inaccessible to the parent program
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
sst
New User
New User
Posts: 5
Joined: Fri Jun 21, 2024 9:56 am

Re: PureBasic Modules: A Quick Tutorial

Post by sst »

In addition to the examples given before, here an example of a very common use case, namely, a structure defined in a module and called in other modules.

Code: Select all

DeclareModule module1
  Structure Writer
    name.s
    surname.s
  EndStructure
    ;.....
EndDeclareModule

Module module1
   ;here can be empty, but it must exist
   ;...... 
 EndModule

DeclareModule module2
   ;...... 
EndDeclareModule

Module module2
  
  Define w1.module1::Writer 
  
  w1\name="Isaac"
  w1\surname="Asimov"
  
  Debug w1\name  ;=Isaac ;it works
  
 ;Define w2.Writer ; ERROR!
  
EndModule
Above, it must be noticed module1:: before structure name.
"module1::Writer" is so called fully-qualified name for structure Writer

By inserting line of code: UseModule module1, in declaration of module3, then
"module1::" before structure name Writer may be left out, meaning that fully-qualified name for structure Writer is not required in this case
See below:

Code: Select all

DeclareModule module3
  UseModule module1  
   ;...... 
EndDeclareModule

Module module3
  
  Define w1.Writer 
  ;it works because line of code: "UseModule module1 " (where structure Writer is defined)
  ;is inserted in  module3 declaration
  ;As shown in previous posts,there is no confict between name of the variables w1 from module2 and w1 from module3,
  ;because they are used inside different modules 
  
  w1\name="Mark"
  w1\surname="Twain"
   
  Debug w1\name ;=Mark, it works
  
EndModule

Post Reply