#124309 - 2004-08-02 11:44 PM
How often may I declare vars?
|
GyroTwister
Fresh Scripter
Registered: 2003-01-29
Posts: 15
Loc: Hoogeveen, The Netherlands
|
Hello, KiX community,
I've got a new problem at hand. Whilst writing my first UDF, I read the How to make a UDF by sealeopard . I this 'How To' it was recommended that I wrote the code in most restrictive 'mode' available to KiX.
Also as a comment on an earlier snippet of code from me, there came as comment not to 'double quote' vars and macros.
So I tried to do this, underneath you'll find the code: Code:
;Function CheckLog ; ;ACTION Log date, time, scriptname and logtext to registry ; ;AUTHOR P.C.E.P.M. Sanders (P_Style; philip@pcsanders.net) ; ;CONTRIBUTORS None ; ;VERSION 1.0 ; ;DATE CREATED 2004/08/02 ; ;DATE MODIFIED 2004/08/02 ; ;KIXTART 4.22 ; ;SYNTAX CheckLog ($LogTxt) ; ;PARAMETERS $LogTxt ; The text you want to have logged into the registry ; ;RETURNS 0 ; ;REMARKS None ; ;DEPENDENCIES None ; Function CheckLog($LogTxt)
Dim $Space, $XName, $LogKey
$LogKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\Logging' $Space = Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) $XName = SubStr ($Script, 1, Len($Script)-4) + Right ($Space, 15 - Len(SubStr ($Script, 1, Len($Script)-4)))
If @ERROR = 0 WriteValue ($LogKey,@DATE+' '+@TIME,$XName+''+$LogTxt,'REG_SZ') Sleep 1 Else Color r+/n Box (4,24,8,45,'Full') Color r+/n At (5,26) 'ERROR ENCOUNTERED!' At (7,26) 'CALL LINE OPERATOR' WriteValue ($LogKey,@DATE+' '+@TIME,$XName+''+$LogTxt+' FAILED!!','REG_SZ') Color w+/n Do Beep Beep Beep Until KBHit() EndIf EndFunction
This is working well.
However I call scripts from a central script. This central script checks the registry wheter or not to install software. You'll find a snippet here: Code:
:Platform ? ' Routines called from loadsetserver.....' $KixScr = ReadValue ($ScrKey,'Platform.kix') If @ERROR = 0 AND $KixScr = 'Installed' GoTo NLSet Else Call 'Z:\Scripts\Platform\Platform.kix' EndIf
:NLSet $KixScr = ReadValue ($ScrKey,'NLSet.kix') If @ERROR = 0 AND $KixScr = 'Installed' GoTo NLApps Else Call 'Z:\Scripts\NLSet\NLSet.kix' EndIf
Before I execute this script I declare global and local vars, call udf's and udl's, set up the console like this: Code:
; ===== OPTIONS / VARIABLES / USER DEFINED FUNCTION / USER DEFINED LIBRARIES ========= ; ----- Set Options ------------------------------------------------------------------ SetOption ('ASCII','ON') SetOption ('HideCursor','ON') SetOption ('WrapAtEOL','ON') SetOption ('Explicit','On') SetOption ('NoVarsInStrings','On') SetOption ('CaseSensitivity','On')
; ----- Call Required UDF's and UDL's ------------------------------------------------ Call 'Z:\SCRIPTS\UDF\CheckLog.UDF'
; ----- Declare Global Variables ----------------------------------------------------- Global $DepKey, $OldKey, $ScrKey, $CurVer, $WinLog, $PolKey, $DefUsr, $Script, $Server
SetConsole ('Maximize')
$DepKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment' $OldKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\OldMachine' $ScrKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\Scripts'
$CurVer = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion' $WinLog = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' $RunKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
$PolKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows'
$DefUsr = 'HKEY_USERS\EYInst'
$Script = @SCRIPTNAME
$Server = '\\LoadSrv'
; ----- Declare Local Variables ------------------------------------------------------ Dim $KixScr, $LoadSetRun
; ----- Set Up Console --------------------------------------------------------------- SetConsole ('Maximize') Color w+/n
; ----- Setting registry keys for installation --------------------------------------- WriteValue ($ScrKey,@SCRIPTNAME,"Busy","REG_SZ")
; ----- Which script is called ------------------------------------------------------- CLS Color y+/n ? ' ' ? ' === ===> Running @SCRIPTNAME <=== ===' Color w+/n
This last section I intend to make this a sort of 'default' for every script which is called. So every script will start with these lines.
Depending on the script local vars will differ.
Is it wise to do so? Is there a better way to make this happen?
I hope you can come up with some answers and maybe raise new questions.
t.i.a.
_________________________
--------------------------------------------------------------
With kind regards,
GyroTwister
|
|
Top
|
|
|
|
#124310 - 2004-08-03 01:08 AM
Re: How often may I declare vars?
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Looks you are on the right track. I am curious about your use of the Z: drive as a location for scripts. I would think that using @Lserver or other variable that uses a UNC instead of a drive letter would be preferable to me.
I would use something like:
for $x=1 to 15 $spaces = $spaces + chr(32) next
instead of your very long line.
|
|
Top
|
|
|
|
#124311 - 2004-08-03 02:41 AM
Re: How often may I declare vars?
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Your subject does not really follow your post. You can declare a variable when it is appropriate to declare a variable. You can use scoping such that a variable inside a loop or script is no longer addressable outside that scope.
See this example of using the same variable in two different scopes.
http://www.kixtart.org/ubbthreads/showthreaded.php?Cat=&Number=98486
|
|
Top
|
|
|
|
#124313 - 2004-08-03 11:41 AM
Re: How often may I declare vars?
|
Anonymous
Anonymous
Unregistered
|
you can try doing a loop: sleep 2 @time " " @msecs ? for $counter=1 to 100000000000 dim $var next @time " " @msecs ?
to see how much it really takes.
|
|
Top
|
|
|
|
#124314 - 2004-08-03 12:44 PM
Re: How often may I declare vars?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Local variables should be automatically destroyed when you leave their context. The resources they used will either be freed and returned, or re-used.
Global variables will remain all the time that the KiXtart executable that they were defined in is running.
With "Explicit" on, you may get an error if you redeclare a variable. To avoid this, choose a naming convention for your global variables which ensures that you are not likely to have a name clash. I used the form "$_filename_variablename" for global variables. Check out the FAQ forum for recommendations.
If you include UDFs, libraries or a common file that defines constants, use a technique which ensures that the variables will only be declared once.
For example, if your UDF is stored in a file called myudf.kix, use something like the following: Code:
; ; MYUDF() - demo UDF file. ; Ensure we are only included once. If IsDeclared($_MYUDF_INCLUDED) Exit 0 EndIf ; Define included global Global $_MYUDF_INCLUDED ; Define globals used by this UDF Global $_MYUDF_Counter ; UDF definition. Function MyUDF() $_MYUDF_Counter=$_MYUDF_Counter+1 "You have called MyUDF "+$_MYUDF_Counter+" times."+@CRLF Exit 0 EndFunction
The IsDeclared() check will ensure that your variables are only declared once. Note, the function itself will be redefined as that happens during the parse phase.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|