|
|
|||||||
Hiya guys (and girls) Hope this hasn't been posted before, looked and couldn't find anything. I'm not a developer by trade, but play around with dev as a hoby... and just love to code. I remember from the times that I did some Turbo Pascal dev that one can compile a Function to be an independent file, and then call it from any other program/script to run and return specific values. I have searched the internet for an explenation of how this works, but all the functions that I find are included in the same script that it is called from. Your view and assistance on this would be greatly appreciated. Thanx |
||||||||
|
|
|||||||
Hi and welcome. The steps are: 1) Create your separate function file and place your function in it. For example, lets say you call it "myfunctions.kix". 2) In your "main" script, place this at the top: CALL "myfunctions.kix" 3) Then just call your function from your main script. |
||||||||
|
|
|||||||
KiX is an interpreter and as such there is no "compiling". Functions are defined into well... UDFs, and these UDFs may be placed into separate files and either CALLed or INCLUDEd. Look up those two commands in the manual to gain an understanding of their difference. |
||||||||
|
|
|||||||
And - sorta 180 degrees from what you are asking... my KGen tool resolves dependencies on the external functions and generates a script with all necessary functions embedded in a single script. This is kind of like the link-editor phase of a compiler. KGen is a free tool, and is available from my web site - look for the KixDev section. I thought you might find it interesting, based on your comments. Glenn |
||||||||
|
|
|||||||
thanx guys I've used both the CALL and INCLUDE commands before... but what I am trying to do is, using the example from Shawn call myfunction(v1,v2) which would then call and run the function, and then return the value of the two varioubles to the script which called the function. In doing this I can then use the variouble values independently in other locations in the script. I have attached some samples of what I intend to do. Please note that the way in which I used the function command is not correct, but the rest is in essence a partial copy of the actual script eg. --------------------Function-------------------- ;Please note that this is merely a section out of a bigger script ;this is why the sub at the bottom of the script might not make sence Code: Function GetInfo($fserver,$lclsite,$lserver) $usrhome = @COMMENT ;Get user home server from AD Select Case ($usrhome = "site1") ;Assign server names and connection $fserver = "\\fileserver1" ;strings Case ($usrhome = "site2") $fserver = "\\fileserver2" Case ($usrhome = "site3") $fserver = "\\fileserver3" Case ($usrhome = "site4") $fserver = "\\fileserver4" EndSelect ;**** Section 2 $section = $section + 1 Gosub GET_LOCATION Select Case ($DefGateway = "10.10.1.1") ;Assign server names and connection $lclsite = "FactoryA" ;strings according to Default $lserver = "\\DC1" ;Gateway Case ($DefGateway = "10.10.2.1") $lclsite = "FactoryB" $lserver = "\\DC2" Case ($DefGateway = "10.10.3.1") $lclsite = "FactoryC" $lserver = "\\DC3" Case ($DefGateway = "10.10.4.1") $lclsite = "FactoryD" $lserver = "\\DC4" EndSelect Goto End ;Analyze location of site through subnet mask :GET_LOCATION CLS $Octet1 = LTrim (SubStr(@IPADDRESS0, 1, 3)) $Octet2 = LTrim (SubStr(@IPADDRESS0, 5, 3)) $Octet3 = LTrim (SubStr(@IPADDRESS0, 9, 3)) $DefGateway = $Octet1 + "." + $Octet2 + "." + $Octet3 + ".1" Return :End EndFunction --------------------Script-------------------- ;this is only a section of the script $F_fserver $F_lclsite $F_lserver ;Get information from Function Call Function GetInfo($fserver,$lclsite,$lserver) $F_fserver = $fserver $F_lclsite = $lclsite $F_lserver = $lserver If Exist "c:\utils\script" ;Test for folder and call Goto next0 ;function to create Else ;if doesn't exist Call $F_fserver + "\nai\scripts\" + createfolder.kix EndIf :Next0 If Exist "k:\" ;Test for mapped drive and Goto Next1 ;call funtion to map if doesn't Else Call $F_fserver + "\nai\scripts\" + $F_lclsite + "\" + Map_All.kix EndIf Hope this helps you to understand what I'm trying to accomplish P.S. This script won't work if copied and pasted as there are some items missing which are still in the actual script. This is merely to give an understanding of what my goals are |
||||||||
|
|
|||||||
Please edit your post and use the CODE TAGS to preserve formatting. Thanks... |
||||||||
|
|
|||||||
Do not pass parms when CALLing. CALL is just to load the UDF file into memory, not to invoke the function. See the FAQ for how to use a function. |
||||||||
|
|
|||||||
Les and Shawn have already given you all the information to do what you've asked, but as you seem to be missing something I'll go over it again with a bit more detail. There are two things to make clear, which should help avoid confusion:
So, how can you create function libraries which you can share between projects? It's very simple.
Before I finish, there is another way to load functions that you might have spotted. I don't think anyone actively uses it, but it's worth mentioning. You'll remember that I said that functions persisted for the entire KiXtart session? Well, that means that if your functions are stored in a file called "myfunctions.udf" and your script is called "myscript.kix" you could access the function from the script by executing it like this: Code: kix32.exe myfunctions.udf myscript.kix The first file myfunctions.udf is loaded, parsed and executed - this creates the functions and GLOBALs in memory. The script finishes and is discarded then the main myscript.kix is loaded, parsed and executed. Because the KiXtart session (kix32.exe) has not exited between the two files any GLOBAL variables or functions are still loaded in memory and available. I hope that all makes sense, and puts you on the right path. |
||||||||
|
|
|||||||
Thank you very much for the explanation Richard... makes perfect sence... as soon as I've tested this I'll let you know how it goes. Thanx again |