Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in dynamic variable access in ColdFusion

Below is the code block, I am using.

<cfset variables.test1 = 'interface.temp1'>
<cfset variables.test2 = 'interface.temp2'>
<cfset variables.test3 = 'variables.' & variables.test2>
<cfset variables["#variables.test1#"] = 23>
<cfset "#variables.test3#" = 50>

<cfdump var="#variables#">
<cfdump var="#variables['interface.temp1']#">

The attached image describes the output, I am getting. The first cfdump displays the "interface.temp1" value as "undefined" and in the second cfdump it is displaying the value as "23". If you also look at the first cfdump, it is displaying all the variables in VARIABLES scope but there is no variable having the value as "23", then in the second cfdump from where is it getting the value as "23"?

Please help me to know the cause behind this.

like image 446
B S Nayak Avatar asked Apr 16 '14 10:04

B S Nayak


1 Answers

I've cracked it. I write it up fully on my blog: "Odd behaviour with struct keys with dots in their names".

Basically once you have a substruct called interface, which is - as @Leigh said - created by this:

<cfset "#variables.test3#" = 50>

Then ColdFusion (and, fwiw, Railo), get confused when trying to resolve struct keys that are prefixed with interface.: it sees those references as references to keys within the interface sub struct, rather than simply key names.

If you don't have that line of code above, then CF's able to see the dotted-key-named value just fine.

like image 50
Adam Cameron Avatar answered Sep 17 '22 13:09

Adam Cameron