Before a procedure or function can be executed, it must be compiled. When a system routine (a function or procedure built into IDL, such as PLOT) is called, either from the command line or from another procedure, IDL already knows about this routine and compiles it automatically. When a user-defined function or procedure is called, IDL must find the routine and then compile it. Compilation can be either automatic or manual, as described below.
When you enter the name of an uncompiled user-defined routine at the command line or call the routine from another routine, IDL searches the current directory for
.sav, where filename is the name of the specified routine. If no file is found in the current directory, IDL searches each directory specified by
If no file matching the routine name is found, IDL issues an error:
% Attempt to call undefined procedure/function: 'routine'
where routine is the name of the routine you specified.
If a file is found, IDL automatically compiles the contents of the file up to the routine whose name matches the name of the file (excluding the suffix), and then executes the routine. If the file does not contain the definition of a routine whose name matches the name of the file, IDL issues the same error as when the no file with the correct name is found.
For example, suppose a file named
proc1.pro contains the following procedure definitions:
PRO proc1 PRINT, 'This is proc1' END PRO proc2 PRINT, 'This is proc2' END PRO proc3 PRINT, 'This is proc3' END
If you enter
proc1 at the IDL command line, only the
proc1 procedure will be compiled and executed. If you enter
proc3 at the command line, you will get an error informing you that you attempted to call an undefined procedure.
In general, the name of the IDL program file should be the same as the name of the last routine within the file. This last routine is usually the main routine, which calls all the other routines within the IDL program file (or, in the case of object classes, the class definition). Using this convention for your IDL program files ensures that all the related routines within the file are compiled before being called by the last main routine.
Many program files within the IDL distribution use this formatting style. For example, open the program file for the XLOADCT procedure,
xloadct.pro, in the IDL Editor. This file is in the
lib/utilities subdirectory of the IDL distribution. This file contains several routines. The main routine (XLOADCT) is at the bottom of the file. When this file is compiled, the IDL Output Log notes all the routines within this file that are compiled:
IDL> .COMPILE XLOADCT % Compiled module: XLCT_PSAVE. % Compiled module: XLCT_ALERT_CALLER. % Compiled module: XLCT_SHOW. % Compiled module: XLCT_DRAW_CPS. % Compiled module: XLCT_TRANSFER. % Compiled module: XLOADCT_EVENT. % Compiled module: XLOADCT.
Note that the main XLOADCT procedure is compiled last.
There are several ways to manually compile a procedure or function.
.COMPILEexecutive command at the IDL command line:
where myFile is the name of a
.pro file located either in IDL's current working directory or in one of the directories specified by
.pro file will also be opened in the IDL Editor.
.RNEWexecutive command at the IDL command line:
where myFile is the name of a
.profile located either in IDL's current working directory or in one of the directories specified by
!PATH.All the routines included in the specified file will be compiled, and any $MAIN$ level programs will be executed automatically. If you are using the IDL Development Environment, the
.profile will also be opened in the IDL Editor.
.COMPILEexecutive command with no filename argument. Invoking any of these executive commands with no filename allows you to interactively create and compile a $MAIN$ level program. See Main-Level Programs for additional details.
.profiles can be compiled using the manual compilation mechanisms. Attempting to compile a .sav file using one of these mechanisms will result in an error.
In the "Hello World" example shown in Creating a Simple Program, we have a user-defined procedure that contains a call to a user-defined function. If you enter the name of the user-defined procedure,
hello_main, at the command line, IDL will compile and execute the
hello_main procedure. After you provide the requested input, a call to the
hello_who function is made. IDL searches for
hello_who.pro, and compiles and executes the function.
If an error occurs during compilation, the error is reported in the Output Log of the IDLDE. For example, because the END statement is commented out, the following user-defined procedure will result in a compilation error:
PRO procedure_without_END PRINT, 'Hello World' ;END
When trying to compile this procedure (after saving it into a file named
procedure_without_END.pro), you will receive the following error in the IDL Output Log:
IDL> .COMPILE procedure_without_END % End of file encountered before end of program. % 1 Compilation errors in module PROCEDURE_WITHOUT_END.