I was frustrated yesterday by the inability to get at the "local function" scope in ColdFusion 8. In ColdFusion 9 each function has a scope called "local" that functions like the various other scopes in ColdFusion. You can loop through its keys and do things with its properties. In ColdFusion 7 and 8 a great many developers actually created a pseudo local scope by doing something like "CFSET var local = structNew()" and then appending all the needed vars to this structure. In CF 9 the "local" scope was already set up for you so if you did "CFSET var x = 10" you had a var called "local.x" to work with as a member of the local object.
In the ColdFusion 8 code I was working with yesterday I had a rather long and involved function that I was trying to modify. I needed to loop through the "local" scope for one reason or another and the there was not "pseudo" local scope created. I did not want to go back and re-engineer the entire function. I was just reviewing and prototyping and there were around 25 local vars declared. So I asked the question to my list - how do I get at that local function scope without refactoring the whole function?
ColdFusion Guru Jared Rypka-Hauer came up with this undocumented solution.
Sure enough, this code creates an object with all the local scope vars in it. There are a couple of caveats.
Finally, here's a little test function you can use to demonstrate to yourself how this works.
Here's that function in action - Local Scope.