REPL in ColdFusion (2018 release)

July 12, 2018
Newbie 32 posts
Followers: 0 people
2

REPL in ColdFusion (2018 release)

Newbie 32 posts
Followers: 0 people
July 12, 2018

In the 2016 release of ColdFusion, we had introduced support for Command Line Interface. In this release, we have introduced support for Read-Eval-Print-Loop (REPL). REPL, a shell, is an interactive programming environment that takes single user inputs, evaluates them, and returns the result to the user. A REPL is like command line shells.

How to start ColdFusion in REPL mode?

Open a terminal and navigate to <ColdFusion Home Directory>\cfusion\bin>

Enter cf.bat/cf.sh. It opens REPL mode in the terminal.

What all can I run in the REPL mode?

You can execute snippets of code/Single line of code or multiple lines of code/or can execute a cfm.

How do I run a cfm file in REPL mode? Is it different from CLI?

Invoking a cfm from REPL is same as you invoke from CLI. You can specify the path of a cfm in REPL mode and execute it.

For example,

cf-cli>C:\ColdFusion\cfusion\wwwroot\run.cfm

Should I use REPL or CLI?

To execute for the first time, both CLI and REPL take the same time.

Subsequently, REPL is faster than CLI because REPL runs within the same VM without having to reinitialize the services.

What are a few optimal use cases of CLI/REPL?

  1. You can automate setting up of the server using administrator
  2. Rapidly try out a snippet of code
  3. Run a cfm page without starting the server

What are the features in REPL?

  1. The settings of the server are used for the code to execute. For example, if the Null support setting is disabled, the same setting would be used here as well.
  2. Color-coded single line exception messages are thrown when there is an exception while executing.
  3. Supports multi-line execution of code.
  4. You can invoke cfms even from a REPL mode

Are changes executed by Admin APIs from CLI/REPL applicable in server?

A CLI instance and the server share the same configuration files. Any admin setting changed from the CLI are reflected in the CLI immediately. The changes also apply to the server. However, to reflect the changes in the ColdFusion Administrator Pages, restart the server. Any changes to settings done from ColdFusion administrator applies to CLI as well. Restart CLI, to reflect the changes.

Show me an example

You can execute Admin APIs through CLI as well as in REPL mode. For example,

<cfscript>

createObject(“component”,”cfide.adminapi.administrator”).login(“admin”);

//instantiate caching object

myObj = createObject(“component”,”cfide.adminapi.debugging”);

//get all IP List

returnValue = myObj.getIPList();

//set IP

myObj.setIP(“10.10.10.10”); myObj.setIP(“22.22.22.22”);

//get all IP List

returnValue = myObj.getIPList();

//delete IP

myObj.deleteIP(“10.10.10.10”);

myObj.deleteIP(“22.22.22.22”);

returnValue = myObj.getIPList();

</cfscript>

How do I run a single-line code?

On entering a REPL mode by invoking cf.bat, a terminal opens, which waits an input. Type-in a line of code that can be executed independently it-self or based on the previously run executions.

For example, enter timenow = now()

In the next line of execution, you can use this variable set in the previous command.

Can I execute a tag-based syntax in a REPL  mode?

No, you can only use the script style syntax from REPL mode.

However, if you want to execute tag-based syntax, you can save that into a cfm file and still execute the same from REPL mode.

How do I run a cfm file with arguments in REPL mode?

You can specify the path of a cfm followed by arguments in REPL mode and execute it. Like CLI, this also handles both positional and named arguments to the cfm.

For example,

cf-cli>C:\ColdFusion\cfusion\wwwroot\run.cfm arg1 arg2

How do I execute multiple lines of executions in REPL mode?

You can type in multiple lines in the REPL mode. A multi-line can be introduced in two ways:

  1. Auto-detection of logical blocks

If you type function add(){ in the REPL mode and press Enter, it will automatically go to the next line and waits for the next line of input in the console.

  1. Adding ^ (caret)

Add ^ at the end of line, and if you hit Enter, it will go to the next line and waits for the next line of input. You can use this when you have a line to type in which doesn’t have any logical block opening( { / ( )

When the input line has any logical block opening, you need not type in ^. Use it when you don’t have it.

For example, in a line you just want to type in ‘function add()’ and ‘{‘ in the next line, you have to type as the line as function add()^

For example,

cf-cli>x=1

1

cf-cli>if (x==1)^

…writeOutput(“Hello ColdFusion”)

Hello ColdFusion

While executing multi-lines from REPL, how do I exit if I decide to not to proceed with the execution?

You can exit multi-line by typing in multilineexit in the new line.

What are the ways of exiting from REPL?

To exit the REPL mode,

  • enter q
  • type in Ctrl+C

When does the output gets printed to the console in REPL mode?

  1. When the typed code is an assignment
  2. An expression evaluating to some value
  3. If it is a function, it is evaluated and if it has return value it is printed

How can I disable automatic printing of output in the console?

In cfusion/bin/cf.bat file, change setting PRINT_REPL_VARS_AUTO to false

Once you set it to false, unless you print it yourself, it won’t print for the cases mentioned above.

How do I set the working directory different than the cfusion\bin directory?

You can do this by navigating to the directory that you want as custom working directory and start cf.bat from there as follows:

C:\apps\Adobe\ColdFusion\2018\express\cfusion\wwwroot\REPL\example>..\..\..\bin\cf.bat

In this case, C:\apps\Adobe\ColdFusion\2018\express\cfusion\wwwroot\REPL\example becomes your custom working directory.

Can I refer cfcs relative to the custom directory referred above?

Yes. The working directory from where cf.bat is started is treated as a reference point for searching cfcs when they are referred in CLI/REPL.

How do I see all commands in the REPL mode?

Type help in the command-prompt for all REPL usage options.

Should ColdFusion server be running for executing commands in REPL?

REPL works even if the ColdFusion server is NOT up and running

Are semicolons optional in REPL code?

A semicolon is optional at the end of the line. When a single line is typed, it is evaluated and printed.

Can I redirect output and error separately?

Standard output and errors can be separately redirected to different files.

Are all features in CLI available in REPL mode also?

Yes, they are available.

Comments (2)
2018-09-24 00:34:01
2018-09-24 00:34:01

Apart from repl which is great, the [adobe coldfusion] product should be renamed to [adobe confusion].

A tool built for the web for which the support pages present code non formatted, without highlighting and without indentation.

Really guys? even with min-effort this should be priority one for support pages.

Like
(1)
>
Petros Makris
's comment
2018-09-25 12:52:47
2018-09-25 12:52:47
>
Petros Makris
's comment

Petros, I think you’re reaching here.

For one thing, other recent blog posts from Adobe about new features in CF2018 have indeed had indentation of code (though not color coding, which would be nice but is not imperative). See for instance:

https://coldfusion.adobe.com/2018/07/oop-and-coldfusion-part-2/

Second, the extended code example above (about using CFSCRIPT) really has nothing that logically needed to be indented.

Third, if you may be referring to the if statement, that’s discussing how to continue on a new line what logically should be on the previous line.

Indeed, and finally: this is a post about using a command-line REPL. Do you really bother with indentation when typing code at the command line?

Like
Add your comment