Page 1 of 1 1
Topic Options
#199625 - 2010-08-19 10:38 AM How to include functions?
Christof Offline
Fresh Scripter

Registered: 2003-04-10
Posts: 30
Loc: Ornbau, Frankonia, Germany
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

Top
#199626 - 2010-08-19 10:47 AM Re: How to include functions? [Re: Christof]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4613
Loc: The Netherlands
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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#199627 - 2010-08-19 11:24 AM Re: How to include functions? [Re: Mart]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4945
Loc: Leatherhead, Surrey, UK
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.

Top
#200024 - 2010-09-23 12:31 PM Re: How to include functions? [Re: Richard H.]
Christof Offline
Fresh Scripter

Registered: 2003-04-10
Posts: 30
Loc: Ornbau, Frankonia, Germany
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").


Edited by Christof (2010-09-23 12:37 PM)

Top
#200025 - 2010-09-23 01:09 PM Re: How to include functions? [Re: Christof]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4613
Loc: The Netherlands
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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200026 - 2010-09-23 01:41 PM Re: How to include functions? [Re: Mart]
Christof Offline
Fresh Scripter

Registered: 2003-04-10
Posts: 30
Loc: Ornbau, Frankonia, Germany
Thank you! Now it works!
Top
#200028 - 2010-09-23 03:01 PM Re: How to include functions? [Re: Christof]
Razer Offline
Fresh Scripter
*

Registered: 2010-09-03
Posts: 8
Thanks! Mart i am also facing the same problem and i searched for this help in internet but it is fulfilled here.
_________________________
Signature removed by admin

Top
#200033 - 2010-09-23 03:26 PM Re: How to include functions? [Re: Christof]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4613
Loc: The Netherlands
 Originally Posted By: Christof
Thank you! Now it works!


Great \:\)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200088 - 2010-09-28 04:47 PM Re: How to include functions? [Re: Mart]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 950
Loc: Stockholm, Sweden.
Nice Mart, looks like a better version of what I tossed up before, I'll sneak that one into my old setup :).
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#200091 - 2010-09-28 05:03 PM Re: How to include functions? [Re: Björn]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4613
Loc: The Netherlands
 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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200092 - 2010-09-28 05:06 PM Re: How to include functions? [Re: Mart]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 950
Loc: Stockholm, Sweden.
lol - that is the one I had. just my variable that diffed :P

Edited by Björn (2010-09-28 05:19 PM)
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 151 anonymous users online.
Newest Members
MikeKrausnick, Ruggiero, xempa333, KroodjeBip, Thala
17470 Registered Users

Generated in 0.036 seconds in which 0.013 seconds were spent on a total of 13 queries. Zlib compression enabled.

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