Ask a Jedi: Find available UDFs

This post is more than 2 years old.

Brandon asks:

Is there a way to see what UDF's are available to the requesting template?

While there is probably some internal method that can do this, there is a little known function you can use to find out if a variable is a UDF. Let's start by creating a bunch of variables and UDFs.

<cfscript> a = 1; b = 2; c = 3; d = 'you betcha!';

function e() { return 'beer!'; }

f = [1,2,3]; g = {name='Ray',consuming=e};

function h() { return "more beer!"; } </cfscript>

<cffunction name="i"> <cfreturn "Stuff"> </cffunction>

Yes, those are horrible variable names, but you get the idea. In the above list, e, h, and i are udfs. Everything else is not. So how can we find them? First lets treat the variables scope like a structure:

<cfloop item="k" collection="#variables#">

Next use the isCustomFunction function:

<cfoutput> The variable #k# is <cfif not isCustomFunction(variables[k])>NOT</cfif> a UDF.<br /> </cfoutput>

That's it! If you run this you will see that it correctly recognizes which variables are UDFs.

In case you're curious, there isn't a direct way to tell if "X" is a built in function. You can use getFunctionList, which returns a struct, but you would then obviously need to do a structKeyList followed by a listFindNoCase.

I'll admit to have never using either of these functions in production. Has anyone used them?

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Edward Beckett posted on 10/18/2008 at 8:23 PM

Couldn't <cfdump var="#variables#" Label="Variables"> run on the calling template determine that?


Comment 2 by ike posted on 10/18/2008 at 8:35 PM

Or just structKeyExists(getFunctionList(),"x")

-- that'll be faster than listFindNoCase("x",structKeyList(getFunctionList()))

The old StripTags function has this in it because of changes to regex in CF6

Comment 3 by ike posted on 10/18/2008 at 8:39 PM

@Edward - I think he may be wanting to determine it programmatically in a situation where he's not sure if function "x" will exist when he writes the code, but wants to use it if it's available at run-time.

Comment 4 by Edward Beckett posted on 10/18/2008 at 8:51 PM

@Ike ... gotcha ... I wasn't considering his possible case scenario.

Comment 5 by Raymond Camden posted on 10/18/2008 at 11:12 PM

@EB - Yes, Ike is right, it was programmatic access.

@Ike - Duh. Can I blame it on being too early in the morning and me having a few too many Newcastles? ;)

Comment 6 by ike posted on 10/18/2008 at 11:26 PM

@Ray - If you want to give people the impression that you load up on ale first thing in the morning, sure. :P

Comment 7 by Edward Beckett posted on 10/18/2008 at 11:28 PM

That might come in real handy if someone wants to determine which rogue UDF's may be causing problems in an application ... very cool.

Comment 8 by Brandon Hansen posted on 10/20/2008 at 5:11 AM

Thanks Ray!
I am going to run that right now. Yeah, the app that I am building allows for custom "plugins" to be installed, and sometimes those plugins need to know if other plugins have been loaded first. So this function will allow them to find out the information.

Comment 9 by Brandon hansen posted on 10/20/2008 at 6:16 AM

Works perfectly! Thanks so much!