#172960 - 2007-01-18 09:45 PM
Re: Compiling a Function and calling it from another script
[Re: Glenn Barnas]
|
Acidean
Lurker
Registered: 2007-01-18
Posts: 3
|
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
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
Edited by Acidean (2007-01-19 08:26 AM)
|
Top
|
|
|
|
#172979 - 2007-01-19 09:50 AM
Re: Compiling a Function and calling it from another script
[Re: Les]
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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:
- 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.
- 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.
- 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.
- 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.
- 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:
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.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 557 anonymous users online.
|
|
|