I recently had a mail exchange with someone who wanted to get scopes by name, and was using undocumented methods on the PageContext to achieve his purpose. While this will work, there is an easier way…
You can do this using the StructGet function. StructGet attempts to gets a value from a named variable path which may traverse several nested structures, for example “my.dog.food”, where both “my” and “dog” are of type Struct; this may even be applied to scopes - “session.dog.food”. As the documentation notes, this function can be dangerous since it will create a path down to a variable if it doesn’t find it, but used carefully it can be incredibly useful.
In this case, the problem can be solved simply by passing the scope name to StructGet:
<cfset myscopename="server"> <cfset myscope=StructGet(myscopename)>
There you go, an abstract technique for getting scopes by name!
Update: The “someone” is none other than Jared, who has blogged about this too.

Raymond Camden | 19-Jul-06 at 11:01 am | Permalink
You can also use this to ‘get’ arrays. Ie, cfset foo = structget(”foo.zoo[2].doo”)
Not that I’d recommend it. ;) I’ve only used structGet in a few places myself. One was scopecache (a caching custom tag) and another was to make a simpler reference to a long variable name.
ashwin | 19-Jul-06 at 1:51 pm | Permalink
Thanks, Ray - point noted - guess I have to go read the doco again too! ;)