$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:
- Since a $MAIN$ program is not named, it cannot be called by other routines. ($MAIN$ programs do not begin with a PRO or FUNCTION declaration that defines the syntax of a named program.)
- A $MAIN$ program is always a procedure because there is no calling program to which a function value could be returned.
- $MAIN$ programs do not accept arguments because no calling programs supply them.
- A $MAIN$ program can read or modify any variable stored in IDL's main scope.
- Only one $MAIN$ program at a time can exist in IDL memory.
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:
Now let IDL know that you want to enter a $MAIN$ program. At the command line, enter:
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:
IDL displays:

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:
At the command line, type:
IDL displays a plot very different from the first one:

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
or choose File → New IDL Source File. In the new file, insert the following lines:
Save the file as ex_main_plot.pro. At the command line, type:
Now, enter the value of the variable A at the command line:
Finally, at the command line, type:
IDL displays:
