The COMPILE_CODE procedure creates and compiles an IDL function or procedure from a scalar string or string array.

Note: Since the code is compiled at run-time, this can be slower than having the code saved in a file. However, COMPILE_CODE can be useful to avoid having to create a new file for a small algorithm, or in cases where you want to dynamically generate a function or procedure from user inputs or variables.

Example


Create an IDL function from a user-supplied math equation:

equation = "sin(x^x)/2^((x^x - !pi/2)/!pi)"
COMPILE_CODE, "function xyzzy,x & return," + equation + " & end"
print, xyzzy([0.5, 1.5, 1.854, 2, 2.257, 2.4])

IDL prints:

0.786014     0.909691  0.000380822    -0.442807  -0.00132630     0.220963

Syntax


COMPILE_CODE, Code

Arguments


Code

A scalar string or string array containing the IDL code for the function or procedure. If Code is a scalar string, then the code should consist of a series of IDL statements separated by ampersand "&" characters. If Code is a string array, then each element should contain a single line of code.

Tip: When passing in a scalar string (consisting of IDL statements separated by "&"), you should avoid using IDL comments, which will cause syntax errors.

Tip: For string arrays, blank lines and IDL comments will be automatically ignored.

Keywords


None.

Examples


Create an IDL function using a string array containing the lines of code:

COMPILE_CODE, [ $
  "function xyzzy,x", $
  " ; lines with comments will be ignored", $
  "return, x + 2", $
  "end"]
xyzzy(10)

IDL prints:

12

Now create two IDL procedures on the fly:

COMPILE_CODE, "pro myprint1,x & print,'Hello, '+x & end"
COMPILE_CODE, [ $
  "pro myprint2,x", $
  " ", $ ; blank lines are also ignored
  "print,'Hello, ' + x", $
  "end"]
myprint1, "world"
myprint2, "IDL users"

IDL prints:

Hello, world
Hello, IDL users

Version History


8.8.3

Introduced

See Also


CALL_FUNCTION, CALL_PROCEDURE, CALL_METHOD, EXECUTE