Page 1 of 1 1
Topic Options
#81943 - 2002-04-05 04:34 AM How to use UDFs
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The purpose of this FAQ is not to teach you how to write UDFs but rather how to incorporate them in your script.
When KiX pre-parses the script, it will catalogue all the UDFs.  They are defined with Function at the start and EndFunction at the end.  When KiX parses through the second time and actually interprets (executes) your script, it will skip over all the UDFs.

Basically, there are two common places to put the UDFs.

1. Place the UDF code directly in your script.  Where in the script doesn't really matter.  Some like it on top while others (me included) prefer the bottom.  Here's a short example using one of sealeopard's UDFs.
Code:
break on

$RC = DiskSpace()

"Disk Space = " + $RC ?

get $_
Exit 1

function diskspace()
if @INWIN=1
$diskspace=getdiskspace(expandenvironmentvars('%SYSTEMROOT%'))
else
$diskspace=getdiskspace('C:\')
endif
endfunction


You may notice that the UDF was placed after the EXIT but that does not prevent it from being pre-parsed.  It could just as easily be place at the top and it would not be necessary to branch around it with a GOTO.


2. Place one or more UDFs into separate library files.  Any extension can be used but I suggest KiX be used for scripts and UDF or KXF reserved for UDF libraries.  These files can then be called from your script just as any sub-script can be called from another script.  In this case however, placement of the call is crucial.  The file that contains the UDF must be called before the UDF can be used.

Code:
break on
;placing the CALL here works
call @ScriptDir + "\diskspace.udf"

$RC = DiskSpace()

"Disk Space = " + $RC ?

get $_
Exit 1

;placing the CALL here doesn't work
call @ScriptDir + "\diskspace.udf"



Edited by Les (2005-09-08 08:21 PM)
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#81944 - 2004-08-20 03:46 PM Re: How to use UDFs
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Note:
UDFs for the most part (at least in the ideal world) are not meant to be modified to make work in your environment. They are meant to work just like built-in KiX functions and should be treated as black boxes. That said, you should look over all UDFs carefully and try to understand what they do before implementing.

If you do modify a UDF, do not expect to get support from the author. If you must modify a UDF, it is important to disclose ALL modifications that were made if you are looking for support.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


Moderator:  Jochen, Radimus, Glenn Barnas, Allen, Arend_, ShaneEP, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 365 anonymous users online.
Newest Members
rrosell, PatrickPinto, Raoul, Timothy, Jojo67
17877 Registered Users

Generated in 0.051 seconds in which 0.021 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org