Home | Back | Contents | Next

# Special Variables and Values

In addition to the scope modifiers: 'this', 'super', 'global', BeanShell supports a number of pre-defined system variables, "magic" values, and methods.

Special Values

- $_ - The value of the last expression evaluated. The strange construct for this is drawn from Perl, but the idea exists in many scripting languages. It is useful for getting back the last result when you are working interactively.
- $_e - The last uncaught exception object thrown. This is useful in interactive use for retrieving the last exception to inspect it for details.
- bsh - The BeanShell root system object, containing system information and variables.
- bsh.args - An array of Strings passed as command line arguments to the BeanShell interpreter.
- bsh.shared - A special static space which is shared across all interpreter instances. Normally each bsh.Interpreter instance is entirely independent; having its own unique global namespace and settings. bsh.shared is implemented as a static namespace in the bsh.Interpreter class. It was added primarily to support communication among instances for the GUI desktop.
- bsh.console - If BeanShell is running in its GUI desktop mode, this variable holds a reference to the current interpreter's console, if it has one.
- bsh.appletcontext - If BeanShell is running inside an Applet, the current applet context, if one exists.
- bsh.cwd - A String representing the current working directory of the BeanShell interpreter. This is used or manipulated by the cd(), dir(), pwd(), and pathToFile() commands.
- bsh.show - A boolean value used by the show() command. It indicates whether results are always printed, for interactive use.
- bsh.interactive - A boolean indicating whether this interpreter running in an interactive mode
- bsh.evalOnly - A boolean indicating whether this interpreter has an input stream or whether is it only serving as an engine for eval() operations (e.g. for embedded use).

Note: The choice of "bsh" for the root system object name was somewhat unfortunate because it conflicts with the current package name for BeanShell (also bsh). This means that if you wish to work with BeanShell classes explicitly from BeanShell scripts (e.g. bsh.Interpreter) you must first import them, e.g.:
        import bsh.Interpreter;
        i=new Interpreter();

## Special Members of 'this' type References

'this' type references have several "magic" members:

- this.variables - An array of Strings listing the variables defined in the current method context (namespace).
- this.methods - An array of Strings listing the methods defined the current method context (namespace).
- this.interpreter - A bsh.Interpreter reference to the currently executing BeanShell Interpreter object.
- this.namespace - A bsh.NameSpace reference to the BeanShell NameSpace object of the current method context. See "Advanced Topics".
- this.caller - A bsh.This reference to the calling BeanShell method context. See "Variables and Scope Modifiers".
- this.callstack - An array of bsh.NameSpace references representing the "call stack" up to the current method context. See "Advanced Topics".

These magic references are primarily used by BeanShell commands.

## Undefined Variables

You can test to see if a variable is defined using the special value void. For example:

    if ( foobar == void )
        // undefined

You can return a variable to the undefined state using the unset() command:

    a == void;  // true
    a=5;
    unset("a"); // note the quotes
    a == void;  // true

## Setting the Command Prompt

Users may set the command line prompt string for use in interactive mode by setting the value of the variable bsh.prompt or by defining the scripted method (or command) getBshPrompt().

If the command or method getBshPrompt() is defined it will be called to get a string to display as the user prompt. For example, one could define the following method to place the current working directory into their command prompt:

    	getBshPrompt() { return bsh.cwd + " % "; }

The default getBshPrompt() command returns the value of the variable bsh.prompt if it is defined or the string "bsh % " if not. If the getBshPrompt() method or command does not exist, throws an exception, or does not return a String, a default prompt of "bsh % " will be used.

Home | Back | Contents | Next