How to use reserved keyword as variable in ColdFusion? - coldfusion

I have to use the reserved keyword "function" as variable name in ColdFusion.
How can I do the same?
I was trying with # symbol but that didn't work.
Any snippet will be helpful

You didn't mention the context of the variable, but here are some observations and possible workarounds:
tag syntax
variable
<!--- works with ACF 10, 11, 2016, 2018 --->
<!--- works with Lucee 4+ --->
<cfset function = "function">
<cfoutput>#function#</cfoutput>
<!--- works everywhere --->
<cfset fieldName = "function">
<cfset variables[fieldName] = "function">
<cfoutput>variables[fieldName]</cfoutput>
function name
<!--- works everywhere --->
<cffunction name="function">
<cfargument name="function">
<cfreturn arguments.function>
</cffunction>
<!--- works with ACF 10, 11, 2016 --->
<!--- doesn't work in Lucee at all --->
<cfoutput>#function("function")#</cfoutput>
<!--- works everywhere --->
<cfinvoke method="function" returnVariable="x">
<cfinvokeargument name="function" value="function">
</cfinvoke>
<cfoutput>#x#</cfoutput>
script syntax
variable
// doesn't work in ACF at all
// works with Lucee 5+
function = "function";
writeOutput(function);
// works everywhere
fieldName = "function";
variables[fieldName] = "function";
writeOutput(variables[fieldName]);
function name
// doesn't work in ACF at all
// works with Lucee 5+
function function(function) {
return arguments.function;
}
// doesn't work in ACF at all
// doesn't work in Lucee at all
wrieOutput( function("function") );
// works in ACF
// doesn't work in Lucee due to a different bug
writeOutput( invoke("", "function", { "function": "function" }) );
In conclusion: Avoid using a reserved keyword! It's inconsistent and leads to preventable bugs.

Related

ColdFusion 10 Error occurring only in IE9

My customer is encountering the error The 2 parameter of the Left function, which is now -1, must be a positive integer after migrating from ColdFusion 8 to ColdFusion 10. The page performs as expected in Firefox 31 and Chrome 36 on Windows 7. However, this error occurs in IE9 but not IE7. Are there different security settings in IE9 that prohibit the processing of the code?
The snippet of code in question is:
<!--- Populate the UserInfo struct --->
<cfloop INDEX="item" LIST="#COOKIE.MYELVIS_USERINFO#" DELIMITERS="&">
<cfset delim = #FindOneOf("=",item)#>
<cfif (len(item)-delim)>
<cfset UserInfo[UCase(left(item,delim-1))] = right(item,len(item)-delim)>
<cfelse>
<cfset UserInfo[UCase(left(item,delim-1))] = "">
</cfif>
</cfloop>
I'm torn to say it's a code error when it works in other browsers, just not IE9. Any thoughts? Thank you.
Perhaps have him clear cookies, he might have a holdover from cf 8 and perhaps the two versions encode differently. It looks like, if this is possible, one of the values doesn't contain a =.
Upon reviewing your code, I think this is what you need.
<cfloop INDEX="item" LIST="#COOKIE.MYELVIS_USERINFO#" DELIMITERS="&">
<cfset delim = #FindOneOf("=",item)#>
<cfif (delim) and (len(item)-delim)>
<cfset UserInfo[UCase(left(item,delim-1))] = right(item,len(item)-delim)>
<cfelse>
<cfset UserInfo[UCase(item)] = "">
</cfif>
</cfloop>

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.
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?
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>

Unable to access variables when converting from application.cfm to application.cfc

I am trying to convert an application I support from application.cfm to application.cfc. I followed Ben Nadel's ColdFusion Application.cfc Tutorial And Application.cfc Reference, but my pages cannot access any of the variables in the APPLICATION scope, without adding APPLICATION. to the variable. As a side note, this application uses 0 cfc's currently.
here is my application.cfc.
<cfcomponent displayname="Application" hint="Handle the application" output="true">
<cfset THIS.Name = "AAS" />
<cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 60, 0 , 0) />
<cfset THIS.SessionManagement = true />
<cfset THIS.setClientCookies = false />
<cfset THIS.versionNum = '1'>
<cfset THIS.genericUserID = 'o005265'>
<cfset THIS.genericPassword = 'zo005265'>
<cffunction
name="OnApplicationStart"
hint="Fires when the application is first created."
access="public"
output="false"
returntype="boolean">
<cfset APPLICATION.aasURL = 'http://127.0.0.1:8500/aaas'>
<cfset APPLICATION.dsn = 've0_aaas'>
<cfset APPLICATION.aas_system = 'development (studio)'>
<cfreturn true />
</cffunction>
</cfcomponent>
Basically I just copied what was in the application.cfm page, and figured it would work the same. I am guessing that I have to invoke this somewhere? That is the only thing that I can think of. Any help would be great.
--- EDIT ---
I have added the OnRequest and OnApplicationStart methods after #EvikJames answer
<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
<!--- Return out. --->
<cfset APPLICATION.aasURL = 'http://127.0.0.1:8500/aaas'>
<cfset APPLICATION.datasource = 've0_aaas'>
<cfset APPLICATION.aas_system = 'development (studio)'>
<cfreturn true />
</cffunction>
<cffunction name="OnRequest" access="public" returntype="void" output="true" hint="Fires after pre page processing is complete.">
<!--- Define arguments. --->
<cfargument name="TargetPage" type="string" required="true">
<!--- Include the requested page. --->
<cfinclude template="#ARGUMENTS.TargetPage#" />
<cfset VARIABLES.dsn = APPLICATION.dsn>
<cfset VARIABLES.aasURL = APPLICATION.aasURL>
<cfset VARIABLES.aas_system = APPLICATION.aas_system>
<!--- Return out. --->
<cfreturn />
</cffunction>
You aren't really trying to use "application" variables (which always need to be scoped). I suspect your old application.cfm page had something like.
<cfapplicatin name="blah"...>
<cfset dsn = 'mydsn'/>
And then you were able to do:
<cfquery datasource="#dsn#">
This approach does not utilze the application scope ... it is merely taking advantage of the fact that your application.cfm always runs no matter what. What it is actually doing is putting variables in the variables scope. Because CF always checks the "variables" scope first you soemthing like #dsn# works - but that is not the same as the application scope.
To mimic this behavior in Application.cfc (as has been suggested) you will need to put your variable in the "onRequest()" function instead of the "onApplicationstart()" function - like so:
<cffunction name="onRequest">
<cfset dsn = 'mydsn'/>
</cffunction>
That is expected. To reference application variables you need to prefix them with application.
In your onApplication start method, do this:
<cfset APPLICATION.datasource = 'MyDSN'>
In your onRequest method, do this:
<cfset VARIABLES.DSN = APPLICATION.datasource>
Then, this query will work:
<cfquery datasource="#dsn#">
// sql
</cfquery>
I should add that when you are fully upgraded, you can remove all of the code above just set the default datasource:
<cfset THIS.datasource = 'MyDSN'>
If the variable is in the application scope, you will always need to identify it that way in your .cfm pages. If you use a variable name without the scope prefix, the application scope is not looked at.
if you want to declare variables in the application.cfc that can be accessed without the application scope in your other pages, simply declare the variables outside of any functions.
<component>
<cfset this.name = "applicationName">
<cfset otherVar = 'something'>
<cfset otherVar2 = 'something else'>
<cffunction name="onApplicationStart>.....</cffunction>
</component>
otherVar and otherVar2 can be called without scope prefix on all .cfm pages.
It sounds like you were not originally using application scoped variables. If the variables were not originally scope with "application." then they were simply in "variables scope" (confusing wording I know) which is accessible by the cfm page hit and others included. That is one big change when moving between application.cfm and application.cfc. The general idea there follows the principle that included CFM files share variables scope and CFC files do not.
If the only change you have to make is changing #dsn# to #appplication.dsn# then just do it and get it over with. There are tools such as WinGrep or even Notepad++ which have find and replace across multiple files.

Upgraded to ColdFusion 9 getting strange cfif error

I have a couple of places that have some code like this:
<cfinvoke component="#application.path#cfc/eval_faculty" method="getPresentations" returnvariable="presentations">
<cfinvokeargument name="id" value="#eval_id#">
<cfinvokeargument name="evalYear" value="#eval_semester#">
<cfinvokeargument name="department" value="#general.dept#">
</cfinvoke>
<cfset prescheck = 0>
<cfloop query="presentations">
<cfif local eq "" and regional eq "" and national eq "" and international eq "">
<cfset prescheck = prescheck+1>
</cfif>
</cfloop>
I get this error:
Complex object types cannot be converted to simple values.
None of these values in the cfif statement is a complex object.
This worked fine in ColdFusion 8. We just upgraded to ColdFusion 9...
The error occurs on the line with <cfif local eq "" ... >
Any ideas?
<cfif local eq ""
It could be that LOCAL is now a system scope in CF9, like FORM, URL, etecetera. So CF complains when you try to perform a string comparison on it because it is a structure. If LOCAL represents a simple variable in your old code, try using a different variable name.
Update: From the comments, if LOCAL is the name of a column in your query, you could either use a sql alias to give it another name:
SELECT Local AS LocalAlias FROM Table
... or use a fully qualified variable name:
<cfif queryName.local ...>

Web Service Error - SAXParseException

Here is my coldfusion web service. When I use soapUI tool to call 'test', i am receiving this
"<ns1:stackTrace xmlns:ns1="http://xml.apache.org/axis/">org.xml.sax.SAXParseException: Premature end of file."
error.
can anyone help me on this issue? I already seached online, but no luck. any code issue?
do I need to use <cfproperty> tag?
<cfcomponent output="false">
<!--- initialisation --->
<cffunction
name="init"
output="false"
hint="return an initialized object.">
<!--- Return THIS reference. --->
<cfreturn THIS />
</cffunction>
<!--- ping --->
<cffunction
name="test"
access="remote"
returntype="numeric"
output="false"
hint="return an true = 0.">
<!--- declare local variables --->
<cfset var local = 0 />
<!--- Return 0. --->
<cfreturn local />
</cffunction>
</cfcomponent>
Couple questions: are you on CF9? What happens when you invoke it straight up (not through SoapUI, but as a http call in a browser)?
Also, I would strongly recommend against using "Local" as a variable name in a function - CF9 introduced some changes in how variables are scoped within a function and uses that as the name for that constrained scope.