$MAIN$ Programs

$MAIN$ programs are generally short programs that can be run from the command line or from a file. The most important difference between $MAIN$ programs and named programs is that $MAIN$ programs have access to variables in the main IDL scope. This means that you can change a variable at the command line, and the $MAIN$ program has access to that changed variable diring the current IDL session.

If you want to run a $MAIN$ program multiple times in different IDL sessions, you may want to save the program in a file.

$MAIN$ programs are different from named programs in the following ways:

When IDL encounters a main program either as the result of a .RUN executive command or in a text file, IDL compiles it into the special program named $MAIN$ and immediately executes it. Afterwards, it can be executed again using the .GO executive command. (See the following examples).

Note
To delete a variable in the main-level scope, use the DELVAR command. To delete all variables at once and reset the session, use the .FULL_RESET_SESSION command.

$MAIN$ Program Examples

This very simple program illustrates how you can use $MAIN$ programs to test results without having to write a procedure or function. The program allows you to change the variable at the command line and run the program again to easily compare the results.

The sine wave plots in these examples are created using the FINDGEN function, which creates a one-hundred-element array, with each element equal to the value of the element's subscript.

Example 1

In the IDL command line, assign the following value to the variable A:

A= 2*!PI/100 * FINDGEN(100) 

Now let IDL know that you want to enter a $MAIN$ program. At the command line, enter:

.RUN 

The IDL command line prompt changes from IDL> to -.

Now you can enter the $MAIN$ program. IDL compiles and runs the program when it encounters the END statement:

DEVICE, RETAIN=2, DECOMPOSED=0 
LOADCT, 0 
PLOT, SIN(A) 
END 

IDL displays:

ex_main_plot01.gif

To run the program again during the same IDL session, type .GO at the command line.

Note
The variable A persists in IDL's $MAIN$ program scope after this routine has finished.

Example 2

Using the same $MAIN$ program as the previous example, we'll change the variable and run it again.

Now, you can return to the command line, change the variable definition, and run the program again to quickly see the difference in the plot.

For example, at the command line, type in a new value for the variable A:

A= 2*!PI/15 * FINDGEN(100) 

At the command line, type:

.GO 

IDL displays a plot very different from the first one:

ex_main_plot02.gif

Example 3

This example is similar to the previous examples, but the code is saved in a file.

In the IDL Workbench, click on the New File icon ed_open_file.gif or choose File →  New IDL Source File. In the new file, insert the following lines:

DEVICE, RETAIN=2, DECOMPOSED=0 
LOADCT, 0 
PLOT, SIN(A) 
END 

Save the file as ex_main_plot.pro. At the command line, type:

.COMPILE ex_main_plot.pro 

Now, enter the value of the variable A at the command line:

A = DIST(15) 

Finally, at the command line, type:

.GO 

IDL displays:

main_run.gif