Acidean
(Lurker)
2007-01-18 06:47 PM
Compiling a Function and calling it from another script

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


ShawnAdministrator
(KiX Supporter)
2007-01-18 07:09 PM
Re: Compiling a Function and calling it from another script

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.





Les
(KiX Master)
2007-01-18 07:09 PM
Re: Compiling a Function and calling it from another 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.

Glenn BarnasAdministrator
(KiX Supporter)
2007-01-18 08:57 PM
Re: Compiling a Function and calling it from another script

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


Acidean
(Lurker)
2007-01-18 09:45 PM
Re: Compiling a Function and calling it from another script

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


NTDOCAdministrator
(KiX Master)
2007-01-18 10:26 PM
Re: Compiling a Function and calling it from another script

Please edit your post and use the CODE TAGS to preserve formatting.

Thanks...


Les
(KiX Master)
2007-01-18 10:28 PM
Re: Compiling a Function and calling it from another script

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.

Richard H.Administrator
(KiX Supporter)
2007-01-19 09:50 AM
Re: Compiling a Function and calling it from another script

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:

  1. There is absolutely no way to directly invoke a function in another file. At runtime KiXtart is only able to invoke functions which are already known and loaded into memory.
  2. Functions are loaded into memory during the file (or "Execute()") parse stage simply by being defined in the script text. Once it is loaded into memory you invoke it simply by name and optionally parameters in parentheses. You never load or invoke functions using CALL, INCLUDE or any other KiXtart statement.


So, how can you create function libraries which you can share between projects?

It's very simple.
  1. Create a function library file with your function(s) in it. It doesn't matter what name you give the file, I use a ".udf" file extension to differentiate it from normal KiXtart scripts. You can also include comments, GLOBAL variable declarations that the functions need and any one-time setup code but be aware that the code will be executed when you call the script to load the the functions into memory, not when you invoke the functions.
  2. In your main script CALL the function library - this CALL will load the functions into memory, declare the GLOBAL variables and execute any setup code. I strongly recommend that you do it as early in the main script as possible, immediately after any SetOption() and local variable declaration. If your script is broken up into multiple files, I'd still recommend that all the function libraries are loaded in the main script, as this avoids issues with duplicate declaration.
  3. Your functions are now loaded into memory, so you can invoke them as often as you like. You don't need to CALL the function library file again.
    Because the functions have a GLOBAL scope they persist for the entire KiXtart session so any CALLed file will be able to use them, as will any additional script files that you specified on the command line.


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.


Acidean
(Lurker)
2007-01-19 12:41 PM
Re: Compiling a Function and calling it from another script

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