Undefined Variables Bug
Posted: Thu Oct 13, 2016 4:39 pm
When specifying text, quotes are used to differentiate it from variables.
Using UDPport = 5001 as an example, html "UDPport" (in quotes) returns the specified text "UDPport", whereas html UDPport (no quotes) returns the value 5001 which was assigned to the variable UDPport.
But there is a bug when specifying a variable that does not exist, because any reference to an undefined variable turns it into a text variable which contains it's own previously undeclared name as its value.
For instance, if not previously defined, testing if udpport == "" would create udpport as a text variable and assign it the value of "udpport".
This has significance when testing for variables, because the very act of testing for a variable (such as after trying to read variables from flash) will cause a previously non-existing variable to then exist with that name and to have the same text value as the new name.
This prevents any subsequent numeric variable assignment of that name, and causes any subsequent numeric operations with that variable name to fail with 'Comparaison between string and number' error.
For instance, testing for an empty udpport numeric variable using if udpport == "" will not work if the variable has not yet been defined, because it will create a new text variable of that name and assign it the value of "udpport". So it will not then be possible to subsequently define eg: let udpport = 5001, and any subroutines that try to do anything with the numeric port variable will fail with the dreaded 'Comparaison between string and number' error.
I think it would be more appropriate for an undefined non-quotes variable to simply return an empty "" (nul) value rather than echoing back the unrecognised name as a text value.
This could then allow testing variables for empty using: if udpport = "", followed by a subsequent assignment of eg: let udpport = 5001, if needed (I'm fairly sure this was how things used to work).
Using UDPport = 5001 as an example, html "UDPport" (in quotes) returns the specified text "UDPport", whereas html UDPport (no quotes) returns the value 5001 which was assigned to the variable UDPport.
But there is a bug when specifying a variable that does not exist, because any reference to an undefined variable turns it into a text variable which contains it's own previously undeclared name as its value.
For instance, if not previously defined, testing if udpport == "" would create udpport as a text variable and assign it the value of "udpport".
This has significance when testing for variables, because the very act of testing for a variable (such as after trying to read variables from flash) will cause a previously non-existing variable to then exist with that name and to have the same text value as the new name.
This prevents any subsequent numeric variable assignment of that name, and causes any subsequent numeric operations with that variable name to fail with 'Comparaison between string and number' error.
For instance, testing for an empty udpport numeric variable using if udpport == "" will not work if the variable has not yet been defined, because it will create a new text variable of that name and assign it the value of "udpport". So it will not then be possible to subsequently define eg: let udpport = 5001, and any subroutines that try to do anything with the numeric port variable will fail with the dreaded 'Comparaison between string and number' error.
I think it would be more appropriate for an undefined non-quotes variable to simply return an empty "" (nul) value rather than echoing back the unrecognised name as a text value.
This could then allow testing variables for empty using: if udpport = "", followed by a subsequent assignment of eg: let udpport = 5001, if needed (I'm fairly sure this was how things used to work).