Oh dear, a dodgy old piece of code in ColdFusion 8 style with local scopes!

A seldom used piece of code fell victim to the "new" ColdFusion "Local scope" from CF 9 (Centaur)

With an Application.cfc; naturally everything inside it is local'ish when the onRequest exists:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
    <cffunction name="OnRequestStart" access="public" returntype="boolean" output="false">
        <cfargument name="TargetPage" type="string" required="true"    />
        <cfreturn true />
    </cffunction>
    <cffunction name="OnRequest" access="public" returntype="void" output="true">
        <cfargument    name="TargetPage" type="string" required="true" />
        <cfinclude template="#arguments.TargetPage#" />
        <cfreturn />
    </cffunction>
</cfcomponent>


Suppose a simple file called ohdear.cfm

<cfset local.myvar = "abc">


included from a file here say runme.cfm:

<cfset url.myvar= "123">
<cfoutput>#myvar#</cfoutput><!--- outputs 123 --->
<cfinclude template="ohdear.cfm">
<cfdump var="#local.myvar#"><!--- outputs abc --->
<cfoutput>#myvar#</cfoutput><!--- lookup to the url scope, whoops, local is first, outputs abc, Doh! --->

So the output is: 123 abc abc


If only an Application.cfm, with no Application.cfc, then the result is completely different: 111 abc 111

Must have been a stroke of genius to think that it would be cool to use local as a scope within an include file :)

FYI; according to CF 9 and the documentation; the scope precedence is :

  1. Local (function-local, UDFs and CFCs only)
  2. Arguments
  3. Thread local (inside threads only)
  4. Query (not a true scope; variables in query loops)
  5. Thread
  6. Variables
  7. CGI
  8. Cffile
  9. URL
  10. Form
  11. Cookie
  12. Client




Comments

Popular posts from this blog

Global SQL Procedure, System Objects and sp_ms_marksystemobject

cf_sql_timestamp vs cf_sql_date vs getdate()

Lucee 4.5.2 cfpdfparam difference with Adobe ColdFusion