Empty string variable - AHK accesses environment variable for function calls.

Get help with using AutoHotkey and its commands and hotkeys
hobboy
Posts: 41
Joined: 05 Jan 2016, 09:59

Empty string variable - AHK accesses environment variable for function calls.

14 Oct 2016, 21:01

Code: Select all

#Warn
username := ""
new_var := Trim(username)
FileAppend % new_var "`n",*
With the above code I ran into the message
"Warning: An environment variable is being accessed; see #NoEnv. Specifically: username" (line 3)
Is this the correct behaviour for passing an empty string variable to a function? Setting #NoEnv will avoid the issue.
Last edited by hobboy on 14 Oct 2016, 23:57, edited 1 time in total.
User avatar
Masonjar13
Posts: 1473
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Trim tries to access environment variable when input is empty string

14 Oct 2016, 22:52

Open a command prompt and type echo %username%. By default, there is no isolation from AHK and environment variables, which is why there's #NoEnv. That directive disables use of environment variables. Yes, this is correct behavior.
OS: Windows 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig
hobboy
Posts: 41
Joined: 05 Jan 2016, 09:59

Re: Trim tries to access environment variable when input is empty string

14 Oct 2016, 23:56

Thanks for the response Masonjar13. I'd just like to clarify a little more how empty strings in variables work.

Code: Select all

#Warn
username := "a"
FileAppend % username "`n",*
username := ""
FileAppend % username "`n",*
The first FileAppend does not give a warning, however the 2nd does. So if I assign a variable an empty string "" this is the same as the variable not being declared? The documentation mentions 'empty strings' but I gather they are the same as the variable itself being empty/undeclared.

If this is the case then I really need to enforce #NoEnv, I could change the variable name but it's possible I could run into another environment variable in my other code.
User avatar
Masonjar13
Posts: 1473
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Empty string variable - AHK accesses environment variable for function calls.

15 Oct 2016, 00:50

By default, all variables act as strings. Variables can not be deinitialized. You can see this by opening the script (double-click the tray icon) and viewing the variables. However, if you simply try a different, non-environment name, you'll see that no error will incur. I can't explain the consistency of the errors though, as I haven't actually looked through the source of the interpreter.
OS: Windows 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Empty string variable - AHK accesses environment variable for function calls.

15 Oct 2016, 03:04

It behaves as intended:
#NoEnv wrote:Specifying the line #NoEnv anywhere in a script prevents empty variables from being looked up as potential environment variables.

Code: Select all

username := "" ; username is empty afterwards
But I think the script shouldn't use the environment variable after an assignment which already created a script variable with the same name. Obviously, username := "a" doesn't set the environment variable:

Code: Select all

#Warn
username := "a"
MsgBox, % username
username := ""
MsgBox, % username
lexikos
Posts: 6497
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Empty string variable - AHK accesses environment variable for function calls.

15 Oct 2016, 06:46

#NoEnv has been recommended (and present in the "New AutoHotkey Script" template) for a very long time.

AutoHotkey did not originally have the capability to differentiate between initialised variables and uninitialised variables; there were just empty variables and non-empty variables. It's rather late to change the behaviour -- if we are going to change it, it would make more sense to simply make #NoEnv the default. v2 does, and I think AutoHotkey_H (including v1) does.

Obviously the behaviour (after explicitly assigning an empty string) is confusing and error-prone; that's why there's a warning.
just me wrote:after an assignment which already created a script variable
The assignment doesn't create a script variable, just initialises it.
Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches.
Source: Functions

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Dysphoria, Google [Bot], Odlanir and 27 guests