"local" scope in CF9 - will it come to Railo?

111 views
Skip to first unread message

Paolo Broccardo

unread,
Nov 18, 2009, 11:33:53 PM11/18/09
to Railo
Hi

In CF9, when you declare local variables within a function, they are
placed in the local scope. The following works:
<cfset var Content1 = "" />
<cfset var Content2 = "" />
<cfdump var="#local#"><cfabort>

You will then see the two content variables in the local scope that is
dumped.
In Railo it fails with the message: "variable [LOCAL] doesn't exist"

Is this going to be made available in Railo?

Thanks

AJ Mercer

unread,
Nov 18, 2009, 11:42:10 PM11/18/09
to ra...@googlegroups.com
you can skip the 2 var lines
and just use
    <cfset local.Content1 = "" />
as well

2009/11/19 Paolo Broccardo <ad...@cheekyonline.com>

--

You received this message because you are subscribed to the Google Groups "Railo" group.
To post to this group, send email to ra...@googlegroups.com.
To unsubscribe from this group, send email to railo+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/railo?hl=.





--
AJ Mercer
http://webonix.net
http://twitter.com/webonix

Paolo Broccardo

unread,
Nov 18, 2009, 11:54:58 PM11/18/09
to Railo
Hi AJ

Thanks - yeah, I'm aware of that - just wondering about legacy code
already using the var keyword. :)



On Nov 19, 6:42 am, AJ Mercer <ajmer...@gmail.com> wrote:
> you can skip the 2 var lines
> and just use
>     <cfset local.Content1 = "" />
> as well
>
> 2009/11/19 Paolo Broccardo <ad...@cheekyonline.com>
>
>
>
> > Hi
>
> > In CF9, when you declare local variables within a function, they are
> > placed in the local scope. The following works:
> > <cfset var Content1 = "" />
> > <cfset var Content2 = "" />
> > <cfdump var="#local#"><cfabort>
>
> > You will then see the two content variables in the local scope that is
> > dumped.
> > In Railo it fails with the message: "variable [LOCAL] doesn't exist"
>
> > Is this going to be made available in Railo?
>
> > Thanks
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Railo" group.
> > To post to this group, send email to ra...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > railo+un...@googlegroups.com <railo%2Bunsu...@googlegroups.com>.

Sean Corfield

unread,
Nov 19, 2009, 12:00:06 AM11/19/09
to ra...@googlegroups.com
On Wed, Nov 18, 2009 at 8:33 PM, Paolo Broccardo <ad...@cheekyonline.com> wrote:
> You will then see the two content variables in the local scope that is
> dumped.
> In Railo it fails with the message: "variable [LOCAL] doesn't exist"

The CFML2009 spec has 'local' scope but it isn't well-specified and it
looks like Adobe's implementation is pretty buggy based on some of the
blog posts I'm seeing out there.

Yes, local scope will come to Railo at some point but right now it's
not clear exactly how it should behave.

Consider this common CFML idiom:

<cfset var local = structNew() />
<cfset var data = 42 />

<cfset local.data = data + 1 />

Now consider this code:

<!--- getSettings() provides default data value --->
<cfset var local = getSettings() />
<cfset var data = local.data + 1 />

This fails on CF9.

How do you think these two pieces of code should behave?
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies US -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Barney Boisvert

unread,
Nov 19, 2009, 12:55:02 AM11/19/09
to ra...@googlegroups.com
It really all depends on whether the local scope retains it's position
as the highest precedence for unscoped variables as it is in CF8 and
older. If it does, then if you use "local.data", that would first
look for a local variable named "local" (because unscoped is highest
priority), and then a "data" key within. If the "local" variable
exists, but doesn't have a "data" key, you get a missing key in struct
exception. If the "local" variable doesn't exist, then you should
check for a local variable named "data" in the local scope (treating
"local" as a scope instead of a variable).

However, from your example, it appears that that's not the case, and
the local scope can NOT be shadowed by a local variable with the name
"local". This seems wrong to me. Line two in your second example
shouldn't be looking for a local variable named "data", it should be
looking for a local variable named "local" with a "data" key. I call
bug in CF9, though I'd be interested to know if this works:

<cfset var local = getSettings() />
<cfset var name = "local.data" />
<cfset var data = evaluate("#name# + 1") />

cheers,
barneyb
> --
>
> You received this message because you are subscribed to the Google Groups "Railo" group.
> To post to this group, send email to ra...@googlegroups.com.
> To unsubscribe from this group, send email to railo+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/railo?hl=.
>
>
>



--
Barney Boisvert
bboi...@gmail.com
http://www.barneyb.com/

Michael Offner-Streit

unread,
Nov 19, 2009, 3:42:42 AM11/19/09
to ra...@googlegroups.com
i'm not a big fan of the current implementation of the local scope in  ACF9 and the hole scope handling in ACF (see "Scope handling in Railo and ACF" below).
but in the end we will have to find a solution that work for most user in the right way.

Railo already has a local scope, the only differents to ACF9 is that you not can access this scope via "local.susi"

i think the best behavior is to use it the following way, then your code works in Railo and ACF (not only 9).
<cffunction name="test">
    <cfset var locally=structNew()>
    <cfset locally.susi=1>
    <cfdump var="#locally#">
</cffunction>

by the way:
in railo administartor at "Settings/Scope" you can set the "Local Scope Mode" from "update" to "always", this change how Railo use the local scope, after this change Railo write every unscoped variable to the local scope, you no longer need the var or local. to write a variable to local scope, this makes code like this a mutch easier:
<cffunction name="test">
    <cfset var qry="">
    <cfset global=1>
    <cfquery name="qry">
    ...
    </cfquery>
</cffunction>
you simply do it this way
<cffunction name="test">
    <cfset variables.global=1>
    <cfquery name="qry">
    ...
    </cfquery>
</cffunction>




Scope handling in Railo and ACF
there is already a know differents in scope handling between ACF and Railo.

in Railo you can never override a scope, in ACF you can inside a function, but only inside a function!

example:
<cfset url="susi">
<cfdump var="#url#">
this set the key "url" inside the variables scope and does not override the urls scope, the dump still return the url scope in Railo and ACF.

BUT this example:
<cffunction name="test">
    <cfargument name="url" default="susi">
    <cfdump var="#url#">
</cffunction>
<cfset test()>

in ACF  the argument key "url" overwrite the url scope, in other word you can no longer access the url scope, the dump return arguments.url.
in Railo the dump still return the url scope, to access arguments.url you have to write <cfdump var="#arguments.url#">

Railo works here not the same way as ACF does, because we think code should not break the enviroment.

or this is even worse
<cfcomponent>
    <cfset this="susi"><!--- in ACF this breaks the component  --->
</cfcomponent>


/micha






Sean Corfield schrieb:
On Wed, Nov 18, 2009 at 8:33 PM, Paolo Broccardo <ad...@cheekyonline.com> wrote:
  
You will then see the two content variables in the local scope that is
dumped.
In Railo it fails with the message: "variable [LOCAL] doesn't exist"
    
The CFML2009 spec has 'local' scope but it isn't well-specified and it
looks like Adobe's implementation is pretty buggy based on some of the
blog posts I'm seeing out there.

Yes, local scope will come to Railo at some point but right now it's
not clear exactly how it should behave.

Consider this common CFML idiom:

<cfset var local = structNew() />
<cfset var data = 42 />

<cfset local.data = data + 1 />

Now consider this code:

<!--- getSettings() provides default data value --->
<cfset var local = getSettings() />
<cfset var data = local.data + 1 />

This fails on CF9.

How do you think these two pieces of code should behave?
  


Sean Corfield

unread,
Nov 19, 2009, 5:51:05 PM11/19/09
to ra...@googlegroups.com
On Wed, Nov 18, 2009 at 9:55 PM, Barney Boisvert <bboi...@gmail.com> wrote:
> <cfset var local = getSettings() />
> <cfset var name = "local.data" />
> <cfset var data = evaluate("#name# + 1") />

Hint: CF9 ignores <cfset var local = ... anything ... />

In my example, getSettings() returned a struct such as { data = 0 }
and in CF8 the code worked. In CF9, the local struct/scope is *empty*
and getSettings() is never called.

Michael Offner-Streit

unread,
Nov 19, 2009, 11:46:42 PM11/19/09
to ra...@googlegroups.com
what do you think about the following feature in the railo 3.2 admin at settings/scope:

handling population of local scope (var local=...)
(x) ignore this expression (CFML Default)
( ) throw a exeption ("it is not allowed to populate the local scope")
( ) write result of assignment to a key "local" inside the local scope

/micha

Sean Corfield schrieb:
On Wed, Nov 18, 2009 at 9:55 PM, Barney Boisvert <bboi...@gmail.com> wrote:
  
<cfset var local = getSettings() />
<cfset var name = "local.data" />
<cfset var data = evaluate("#name# + 1") />
    
Hint: CF9 ignores <cfset var local = ... anything ... />

In my example, getSettings() returned a struct such as { data = 0 }
and in CF8 the code worked. In CF9, the local struct/scope is *empty*
and getSettings() is never called.
  


-- 
Michael Offner-Streit
CTO
Railo Technologies GmbH

Andrew Penhorwood

unread,
Nov 20, 2009, 8:06:44 AM11/20/09
to Railo
I like the feature Micha. I think the way CF9 handles it is just
plain wrong. Most people will spend days trying to figure out why
their code is not working on CF9.

Andrew Penhorwood

On Nov 19, 11:46 pm, Michael Offner-Streit <michael.off...@railo.ch>
wrote:
> what do you think about the following feature in the railo 3.2 admin at
> settings/scope:
>
> handling population of local scope (var local=...)
> (x) ignore this expression (CFML Default)
> ( ) throw a exeption ("it is not allowed to populate the local scope")
> ( ) write result of assignment to a key "local" inside the local scope
>
> /micha
>
> Sean Corfield schrieb:
>
> > On Wed, Nov 18, 2009 at 9:55 PM, Barney Boisvert <bboisv...@gmail.com> wrote:
>
> >> <cfset var local = getSettings() />
> >> <cfset var name = "local.data" />
> >> <cfset var data = evaluate("#name# + 1") />
>
> > Hint: CF9 ignores <cfset var local = ... anything ... />
>
> > In my example, getSettings() returned a struct such as { data = 0 }
> > and in CF8 the code worked. In CF9, the local struct/scope is *empty*
> > and getSettings() is never called.
>
> --
> Michael Offner-Streit
> CTO
> Railo Technologies GmbH
> michael.off...@railo.chwww.getrailo.com

Paul Kukiel

unread,
Nov 20, 2009, 8:27:11 AM11/20/09
to ra...@googlegroups.com
Technically CF9 treat local as a scope and no longer a variable. In this
case the local scope seams to act consistently as other scopes.

I'm not saying this is a good thing as it has broken code but it is
consistent with other scope behavior.

Personally the implicit local scope seams no to be thought out and was it
really so hard to just continue to just:

<cfset var local = {} />

I will continue to use this method so at least my code will work across
multiple engines.

I'm guessing the fix for this will be that behind the scenes they will be
doing a structAppend() for Sean's example

Paul.

Paolo Broccardo

unread,
Nov 22, 2009, 5:35:59 AM11/22/09
to Railo
If Railo doesn't put variables into the local sope when you set the
using the 'var' keyword, then what scope do they go into then?
I presume none? How would you dump you local variables in a function
then?

Sami Hoda

unread,
Nov 22, 2009, 4:13:29 PM11/22/09
to Railo
FYI - The Local Scope issue has been fixed in Adobe CF9 and will be
available in CF9 Updater 1.

Sami

Barney Boisvert

unread,
Nov 22, 2009, 4:27:03 PM11/22/09
to ra...@googlegroups.com
Can you qualify "fixed", maybe with an example? Or are you
specifically referring to the assignment of a bar named "local" as in
Sean's original example?

Thanks,
barneyb

Andrew Scott

unread,
Nov 22, 2009, 10:19:15 PM11/22/09
to ra...@googlegroups.com
No Adobe have ear marked a fix for this bug, it was pointed out to them via blog post that I wrote that there is a major issue in the way this works.
 
<cfset var local = someFunction() />
 
Is very legal and works well in ColdFusion 8, and is another example of broken backward compatability that they needed to fix. This has been ear marked for ColdFusion updater 1, so that means either a hotfix or an update is around the corner from Adobe.
 


 

gary gilbert

unread,
Nov 23, 2009, 9:16:17 AM11/23/09
to ra...@googlegroups.com
A bug that I recently reported has been fixed and earmarked for updater 1.  so I am guessing too, that its in the very near future.


Gary Gilbert
http://www.garyrgilbert.com/blog
Reply all
Reply to author
Forward
0 new messages