#141917 - 2005-06-16 10:53 PM
Re: Correct use of env variable in kix parameter
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
This looks better...
Code:
$rc = READPROFILESTRING("%ProgramFiles%\visionair\visionrecords\file.ini","Section","Key")
|
|
Top
|
|
|
|
#141919 - 2005-06-17 02:59 AM
Re: Correct use of env variable in kix parameter
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
It is considered "best practice" for compliance with NoVarsInStrings and NoMacrosInStrings to concatenate, such as...
Code:
$rc = SetOption("NoVarsInStrings","ON") $rc = SetOption("NoMacrosInStrings","ON") "This is my USERID: " + @USERID ? $sWKSTA = "\\" + @WKSTA + "\ADMIN$" "This is the UNC path to my ADMIN$ Share: " + $sWKSTA ?
|
|
Top
|
|
|
|
#141920 - 2005-06-17 12:28 PM
Re: Correct use of env variable in kix parameter
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Tch! No (env) vars in strings: Code:
$rc = READPROFILESTRING(%ProgramFiles%+"\visionair\visionrecords\file.ini","Section","Key")
|
|
Top
|
|
|
|
#141924 - 2005-06-21 10:12 AM
Re: Correct use of env variable in kix parameter
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
It is bad practice to place variables in strings, whatever the type of variable.
When we say "in strings", we explicitly mean "in string literals", i.e. something that you have typed in between quote characters (" or '). The variable istelf can be used wherever a string literal would be used - just don't put it inside quotes.
When a variable is not used in a string it is expanded, and the value of the variable is used in the expression. If the variable is in quotes this may or may not happen, depending in the variable value and type.
Some variable types (arrays, objects) simply don't work in strings at all. Undeclared variables will return an empty value when not used in strings, but the variable name will be used as-is when used in strings.
See this example: Code:
"Undeclared=$Undeclared" ? "Undeclared="+$Undeclared ?
This displays the following:
Quote:
Undeclared=$Undeclared Undeclared=
Now, one of those results must be wrong!
Also note, when you are using SetOption("Explicit","ON") undeclared variabled will not be caught if they are present in strings.
Not declaring variables in strings future-proofs your code, and means that other people can use your scripts without changing it.
Variables in strings is not forbidden, and support for it is likely to be around for a while. However if you get into the habit of not placing variables in strings you will write code which is more portable, easier to debug and less prone to odd failures.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 996 anonymous users online.
|
|
|