@@ -444,7 +444,7 @@ This will generate:
444444
445445 $ node process-2.js one two=three four
446446 0: node
447- 1: /Users/mjr/work/node_docs/data/v0.1.31/examples /process-2.js
447+ 1: /Users/mjr/work/node /process-2.js
448448 2: one
449449 3: two=three
450450 4: four
@@ -2239,38 +2239,129 @@ The unescape function used by `querystring.parse`, provided so that it could be
22392239## REPL
22402240
22412241A Read-Eval-Print-Loop is available both as a standalone program and easily
2242- includable in other programs.
2242+ includable in other programs. REPL provides a way to interactively run
2243+ JavaScript and see the results. It can be used for debugging, testing, or
2244+ just trying things out.
22432245
22442246The standalone REPL is called ` node-repl ` and is installed at
2245- ` $PREFIX/bin/node-repl ` . It's recommended to use it with the program
2246- ` rlwrap ` for a better user interface. I set
2247+ ` $PREFIX/bin/node-repl ` .
22472248
2248- alias node-repl='rlwrap node-repl'
2249+ mjr:~$ /usr/local/bin/node-repl
2250+ Welcome to the Node.js REPL.
2251+ Enter ECMAScript at the prompt.
2252+ Tip 1: Use 'rlwrap node-repl' for a better interface
2253+ Tip 2: Type Control-D to exit.
2254+ Type '.help' for options.
2255+ node> a = [ 1, 2, 3];
2256+ [ 1, 2, 3 ]
2257+ node> a.forEach(function (v) {
2258+ ... sys.puts(v);
2259+ ... });
2260+ 1
2261+ 2
2262+ 3
22492263
2250- in my zsh configuration.
22512264
2252- Inside the REPL, Control+D will exit. The special variable ` _ ` (underscore) contains the
2253- result of the last expression.
2265+ ### repl.start(prompt, stream)
22542266
2255- The library is called ` /repl.js ` and it can be used like this:
2267+ Starts a REPL with ` prompt ` as the prompt and ` stream ` for all I/O. ` pomrpt `
2268+ is optional and defaults to ` node> ` . ` stream ` is optional and defaults to
2269+ ` process.openStdin() ` .
22562270
2257- var sys = require('sys'),
2258- net = require('net'),
2259- repl = require('repl');
2260- nconnections = 0;
2261- net.createServer(function (c) {
2262- sys.error('Connection!');
2263- nconnections += 1;
2264- c.end();
2265- }).listen(5000);
2266- repl.start('simple tcp server> ');
2267-
2268- The repl provides access to any variables in the global scope. You can expose a variable
2269- to the repl explicitly by assigning it to the ` repl.scope ` object:
2270-
2271- var count = 5;
2272- repl.start();
2273- repl.scope.count = count;
2271+ Multiple REPLs may be started against the same running instance of node. Each
2272+ will share the same global object but will have unique I/O.
2273+
2274+ Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
2275+
2276+ var sys = require("sys"),
2277+ net = require("net"),
2278+ repl = require("repl");
2279+
2280+ connections = 0;
2281+
2282+ repl.start("node via stdin> ");
2283+
2284+ net.createServer(function (socket) {
2285+ connections += 1;
2286+ repl.start("node via Unix socket> ", socket);
2287+ }).listen("/tmp/node-repl-sock");
2288+
2289+ net.createServer(function (socket) {
2290+ connections += 1;
2291+ repl.start("node via TCP socket> ", socket);
2292+ }).listen(5001);
2293+
2294+ Running this program from the command line will start a REPL on stdin. Other
2295+ REPL clients may connect through the Unix socket or TCP socket. ` telnet ` is useful
2296+ for connecting to TCP sockets, and ` socat ` can be used to connect to both Unix and
2297+ TCP sockets.
2298+
2299+ By starting a REPL from a Unix socket-based server instead of stdin, you can
2300+ connect to a long-running node process without restarting it.
2301+
2302+
2303+ ### readline support
2304+
2305+ Interactive command history for REPL is available from external programs like ` rlwrap `
2306+ or ` socat ` . These programs are available from many Unix package managers.
2307+
2308+ To start the standalone REPL with ` rlwrap ` :
2309+
2310+ rlwarp node-repl
2311+
2312+ It might be convenient to use this alias in your shell configuration:
2313+
2314+ alias repl='rlwrap node-repl'
2315+
2316+ Using ` socat ` to connect to a Unix socket:
2317+
2318+ socat READLINE UNIX-CONNECT:/tmp/node-repl-sock
2319+
2320+ Using ` socat ` to connect to a TCP socket on localhost:
2321+
2322+ socat READLINE TCP-CONNECT:localhost:5001
2323+
2324+
2325+ ### REPL Features
2326+
2327+ Inside the REPL, Control+D will exit. Multi-line expressions can be input.
2328+
2329+ The special variable ` _ ` (underscore) contains the result of the last expression.
2330+
2331+ node> [ "a", "b", "c" ]
2332+ [ 'a', 'b', 'c' ]
2333+ node> _.length
2334+ 3
2335+ node> _ += 1
2336+ 4
2337+
2338+ The REPL provides access to any variables in the global scope. You can expose a variable
2339+ to the REPL explicitly by assigning it to the ` scope ` object associated with each
2340+ ` REPLServer ` . For example:
2341+
2342+ // repl_test.js
2343+ var repl = require("repl"),
2344+ message = "message";
2345+
2346+ repl.start().scope.m = message;
2347+
2348+ Things in the ` scope ` object appear as local within the REPL:
2349+
2350+ mjr:~$ node repl_test.js
2351+ node> m
2352+ 'message'
2353+
2354+ There are a few special REPL commands:
2355+
2356+ - ` .break ` - While inputting a multi-line expression, sometimes you get lost or just don't care
2357+ about completing it. ` .break ` will start over.
2358+
2359+ - ` .clear ` - Resets the ` scope ` object to an empty object and clears any multi-line expression.
2360+
2361+ - ` .exit ` - Close the I/O stream, which will cause the REPL to exit.
2362+
2363+ - ` .help ` - Show this list of special commands.
2364+
22742365
22752366
22762367## Addons
0 commit comments