Christof
(Fresh Scripter)
2010-08-19 10:38 AM
How to include functions?

Hello!

My login-script is starting to be confusing, so I start to swap parts of it in own script that I run with form the main-script with the call-command.

But in the main-script are some UDF's that I need in the main-script and also in the sub-scripts. Is it possible to use the UDF's and variables from the main-script also in the the sub-script?

Bye


Mart
(KiX Supporter)
2010-08-19 10:47 AM
Re: How to include functions?

If you call the UDF's from the main script and also call the child scripts from the main script everything will be ok because everything runs in the same kixtart process.

There are several options for calling all functions without errors if you add a new function.

-1 You can put all functions into one separate file and call that file that the top of your script.
-2 You can put all function in separate files and call them one by one by walking through the folder that holds the function files. See below for an example. I have the example below at the top of almost all of my scripts.

 Code:
;Call each *.udf file until an error occurs or there are no more files to call.
$udf = Dir(@SCRIPTDIR + "\functions\*.udf")
While $udf <> "" And @ERROR = 0
	Call @SCRIPTDIR + "\functions\" + $udf
 	;Get next *.udf filename.
	$udf = Dir()
Loop


Richard H.Administrator
(KiX Supporter)
2010-08-19 11:24 AM
Re: How to include functions?

As Mart has said if you define the functions in the main script then they are accessible in the sub-scripts, if you define the functions in the sub-script then they are available in the main script. Functions have a global scope.

There are a couple of things to be watch out for:
  1. If you are using SetOption("Explicit","ON") then don't CALL the scripts which define the UDF more than once, otherwise you will get an error when the script attempts to define the function a second time.
  2. When you define a function in a script it doesn't matter where it is defined - commonly people define them at the end of the script. However if you move the functions into a seperate file then you must CALL the file before you use the functions in the main script.
    You can get around this restriction by using INCLUDE instead of CALL. INCLUDE loads the file during the parsing phase so the sub-script then appears to be part of the main script and processing acts as if it was one single file.


Christof
(Fresh Scripter)
2010-09-23 12:31 PM
Re: How to include functions?

Thanks for your responses!

Now I had time to change my script, but it doesn't work. Until now I had 3 UDF's at the end of my main script. I put them all in a own file (udf.kix) and called this file in the first line of my main script (call "udf.kix"). But the UDF's are not accessible. One of the functions is "SpecialFolders". When I call this function later in the main script, I got this error:

ERROR : unknown command [SpecialFolders]!

I dot' know why...

First I tried it with Include, but there appeared an error, that the file was not found (Include "udf.kix"). I tried it also with path (Include "..\udf.kix").


Mart
(KiX Supporter)
2010-09-23 01:09 PM
Re: How to include functions?

It would better to specify the path.

 Code:
Call @scriptdir + "\functions.kix"


The error you get is typical for a UDF that is not included or called.


Christof
(Fresh Scripter)
2010-09-23 01:41 PM
Re: How to include functions?

Thank you! Now it works!

Razer
(Fresh Scripter)
2010-09-23 03:01 PM
Re: How to include functions?

Thanks! Mart i am also facing the same problem and i searched for this help in internet but it is fulfilled here.

Mart
(KiX Supporter)
2010-09-23 03:26 PM
Re: How to include functions?

 Originally Posted By: Christof
Thank you! Now it works!


Great \:\)


Björn
(Korg Regular)
2010-09-28 04:47 PM
Re: How to include functions?

Nice Mart, looks like a better version of what I tossed up before, I'll sneak that one into my old setup :).

Mart
(KiX Supporter)
2010-09-28 05:03 PM
Re: How to include functions?

 Originally Posted By: Björn
Nice Mart, looks like a better version of what I tossed up before, I'll sneak that one into my old setup :).


Yeah well I did not made this up myself I found something similar here on the board and took copied stole borrowed it. Been using it ever since.


Björn
(Korg Regular)
2010-09-28 05:06 PM
Re: How to include functions?

lol - that is the one I had. just my variable that diffed :P