stubotnik
stubotnik

Reputation: 1982

ColdFusion local scope inside object literal

I'm seeing some weird behaviour when using CF's local scope in an object literal, in function arguments. But only when executed inside a loop...

Example code:

<cffunction name="f">
    <cfoutput>
        <cfset LOCAL.foo = 123 />       

        <!--- Works fine --->
        #serializeJSON({blah = LOCAL.foo})#

        <!--- Works fine --->
        <cfloop from=1 to=1 index="i">
            <cfset bar = {blah = LOCAL.foo} />
            #serializeJSON(bar)#
        </cfloop>

        <!--- Element FOO is undefined in LOCAL --->
        <cfloop from=1 to=1 index="i">  
            #serializeJSON({blah = LOCAL.foo})#
        </cfloop>
    </cfoutput>
</cffunction>


<cfset f() />

PS: serializeJSON() is just for example purposes. This is happening in any function I've tested where one of the arguments is a struct.

Upvotes: 4

Views: 313

Answers (2)

Evik James
Evik James

Reputation: 10473

LOCAL is a scope used only within functions. If you try to create a LOCAL scope variable outside of a function, it will fail.

I will write up a test and prove it to you in a minute....

UPDATE Actually, I have CF 8 at work and can't test it.

In CF8 and below, you can set LOCAL.Foo, but it's not really a CF scope.

In CF9 and above, LOCAL can be set only within a function.

<cffunction>
   <cfset LOCAL.foo = 1>
   <cfreturn LOCAL.foo>
</cffunction>

Upvotes: 1

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

Works just fine in Railo.

It also doesn't make any difference if using any other container instead of local scope, also it's impossible to catch this with cftry.

If you serialize just local scope within the loop:

<cfloop from=1 to=1 index="i">
    #serializeJSON(local)#
</cfloop>

Result is:

{"ARGUMENTS":{},"___IMPLICITARRYSTRUCTVAR1":{"BLAH":123},"___IMPLICITARRYSTRUCTVAR0":{"BLAH":123},"FOO":123} 

Looks like a bug. Mind filing?

Upvotes: 2

Related Questions