Testing CALLER.Scope.Value And Caller[ "Scope.Value" ] In ColdFusion Custom Tags

Posted January 15, 2008 at 8:43 AM

Tags: ColdFusion

I was getting ready to answer an "Ask Ben" question, but I wanted to get a few facts nailed down before I proceeded. In particular, I wanted to double check on how scoped values could be referenced via the CALLER scope of ColdFusion custom tags. Often times, when dealing with ColdFusion custom tags, the references I used in CALLER are either non-scoped (assumed to be in the VARIABLES scope of the calling page) or use a variable name passed into the custom tag itself.

Here, I just wanted to see how referencing scoped variables would interact with the CALLER scope. So, I set up this simple index.cfm ColdFusion page that sets a "Value" into a few different scopes:

 Launch code in new window » Download code as text file »

  • <!--- Set a value into various scopes. --->
  • <cfset REQUEST.Value = "From Request Scope" />
  • <cfset VARIABLES.Value = "From Variables Scope" />
  • <cfset COOKIE.Value = "From Cookie Scope" />
  •  
  • <!--- Test caller references. --->
  • <cf_testcaller />

Then, I set up a really simple ColdFusion custom tag, testcaller.cfm, that simply echoes out these variables by referring to them via the CALLER scope. Now, a long time ago, I discovered that the CALLER scope uses special key values that act differently than any standard ColdFusion structs, so this should be no surprise; but, I just wanted to formally review that this is all good and well:

 Launch code in new window » Download code as text file »

  • <cfoutput>
  •  
  • <p>
  • Dot Notation:
  • </p>
  •  
  • <p>
  • REQUEST .... #CALLER.REQUEST.Value#<br />
  • VARIABLES .... #CALLER.VARIABLES.Value#<br />
  • COOKIE .... #CALLER.COOKIE.Value#<br />
  • </p>
  •  
  • <p>
  • Array Notation:
  • </p>
  •  
  • <p>
  • REQUEST .... #CALLER[ "REQUEST.Value" ]#<br />
  • VARIABLES .... #CALLER[ "VARIABLES.Value" ]#<br />
  • COOKIE .... #CALLER[ "COOKIE.Value" ]#<br />
  • </p>
  •  
  • </cfoutput>
  •  
  •  
  • <!--- Exit out of tag. --->
  • <cfexit method="exittag" />

Notice that we are testing the full dot-notation starting with the CALLER scope and, we are using the entire struct path of the target variable when using the array notation. Running the tag, we get the expected output:

Dot Notation:

REQUEST .... From Request Scope
VARIABLES .... From Variables Scope
COOKIE .... From Cookie Scope

Array Notation:

REQUEST .... From Request Scope
VARIABLES .... From Variables Scope
COOKIE .... From Cookie Scope

Now, just a reminder that this only works with proper variable names. You can't try to do something like this:

 Launch code in new window » Download code as text file »

  • CALLER[ "REQUEST[ 'Value' ]" ]

... even though this is merely an alternate way to reference the REQUEST-scope Value variable.

Not a whole lot going on here; I just wanted to put this up here so I could refer to it later.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page





Reader Comments

Jan 15, 2008 at 11:18 AM // reply »
153 Comments

Hmm. That seems shady to me. What if I do this?

cfset Variables["Request.Value"] = "From Variables Scope"

Then try to read it?

#Caller["Request.Value"]#

Logically, I should then get "From Variables Scope" instead of "From Request Scope", right?

Long story short, coding to count on that behavior seems iffy. And, logically, Caller.Cookie is just Cookie, and Caller.Request is just Request, as the thread should share the Cookie and Request scopes. Or are you trying to do something sneakier?


Jan 15, 2008 at 12:36 PM // reply »
1 Comments

Do you realize that it is a felony offense to use the term "Active Release Technique" (ART), or its likeness, without permission from Dr. Michael Leahy of Colorado Springs, CO? It is a patented technique, and they have/will prosecute. I thought that I might warn you.


Jan 15, 2008 at 12:39 PM // reply »
6,516 Comments

@Tammy,

Oh snap, you just said it without permission!! Now we're both going down.


Jan 15, 2008 at 12:45 PM // reply »
6,516 Comments

@Rick,

Good point on the ambiguous and conflicting variable name access. As far as the fact that CALLER.REQUEST is the same thing as REQUEST, this is true; however, I was setting this up for something a bit more dynamic in which the scope / structure being referenced might not be built-in one. I just used those because it seemed easy to test.

As far as good or bad for programming to this usage, I am not sure I can say one way or the other. Clearly, it doesn't work within the REQUEST scope itself which means that CALLER is not the exact same beast. I don't think that is an issue. CALLER has its own set of rules and behaviors. Unless Adobe says that this is a bug, which I don't think they have, I can't see anything wrong with leveraging it.

Of course, the cleaner, less ambiguous way would probably be to just use the dynamic naming convention:

<cfset "CALLER.#DynamicName#" = "Some Value" />

The problem is, this only works with Setting, not Getting. In fact, I don't think there is a way to actually reference a variable reference without using the CALLER[] notation. Perhaps that is why CALLER is designed to work this way.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »