Next Previous Contents

20. Eval Functions

20.1 _$

Synopsis

Expand the dollar-escaped variables in a string

Usage

String_Type _$(String_Type s)

Description

This function expands the dollar-escaped variables in a string and returns the resulting string.

Example

Consider the following code fragment:

     private variable Format = "/tmp/foo-$time.$pid";
     define make_filename ()
     {
        variable pid = getpid ();
        variable time = _time ();
        return _$(Format);
     }
Note that the variable Format contains dollar-escaped variables, but because the $ suffix was omitted from the string literal, the variables are not expanded. Instead expansion is deferred until execution of the make_filename function through the use of the _$ function.

See Also

eval, getenv

20.2 autoload

Synopsis

Load a function from a file

Usage

autoload (String_Type funct, String_Type file)

Description

The autoload function is used to declare funct to the interpreter and indicate that it should be loaded from file when it is actually used. If func contains a namespace prefix, then the file will be loaded into the corresponding namespace. Otherwise, if the autoload function is called from an execution namespace that is not the Global namespace nor an anonymous namespace, then the file will be loaded into the execution namespace.

Example

Suppose bessel_j0 is a function defined in the file bessel.sl. Then the statement

      autoload ("bessel_j0", "bessel.sl");
will cause bessel.sl to be loaded prior to the execution of bessel_j0.

See Also

evalfile, import

20.3 byte_compile_file

Synopsis

Compile a file to byte-code for faster loading.

Usage

byte_compile_file (String_Type file, Int_Type method)

Description

The byte_compile_file function byte-compiles file producing a new file with the same name except a 'c' is added to the output file name. For example, file is "site.sl", then this function produces a new file named site.slc.

Notes

The method parameter is not used in the current implementation, but may be in the future. For now, set it to 0.

See Also

evalfile

20.4 eval

Synopsis

Interpret a string as S-Lang code

Usage

eval (String_Type expression [,String_Type namespace])

Description

The eval function parses a string as S-Lang code and executes the result. If called with the optional namespace argument, then the string will be evaluated in the specified namespace. If that namespace does not exist it will be created first.

This is a useful function in many contexts including those where it is necessary to dynamically generate function definitions.

Example

    if (0 == is_defined ("my_function"))
      eval ("define my_function () { message (\"my_function\"); }");

See Also

is_defined, autoload, evalfile

20.5 evalfile

Synopsis

Interpret a file containing S-Lang code

Usage

Int_Type evalfile (String_Type file [,String_Type namespace])

Description

The evalfile function loads file into the interpreter and executes it. If called with the optional namespace argument, the file will be loaded into the specified namespace, which will be created if necessary. If given no namespace argument and the file has already been loaded, then it will be loaded again into an anonymous namespace. A namespace argument given by the empty string will also cause the file to be loaded into a new anonymous namespace.

If no errors were encountered, 1 will be returned; otherwise, a S-Lang exception will be thrown and the function will return zero.

Example

    define load_file (file)
    {
       try
       {
         () = evalfile (file);
       }
       catch AnyError;
    }

Notes

For historical reasons, the return value of this function is not really useful.

The file is searched along an application-defined load-path. The get_slang_load_path and set_slang_load_path functions may be used to set and query the path.

See Also

eval, autoload, set_slang_load_path, get_slang_load_path

20.6 get_slang_load_path

Synopsis

Get the value of the interpreter's load-path

Usage

String_Type get_slang_load_path ()

Description

This function retrieves the value of the delimiter-separated search path used for loading files. The delimiter is OS-specific and may be queried using the path_get_delimiter function.

Notes

Some applications may not support the built-in load-path searching facility provided by the underlying library.

See Also

set_slang_load_path, path_get_delimiter

20.7 set_slang_load_path

Synopsis

Set the value of the interpreter's load-path

Usage

set_slang_load_path (String_Type path)

Description

This function may be used to set the value of the delimiter-separated search path used by the evalfile and autoload functions for locating files. The delimiter is OS-specific and may be queried using the path_get_delimiter function.

Example

    public define prepend_to_slang_load_path (p)
    {
       variable s = stat_file (p);
       if (s == NULL) return;
       if (0 == stat_is ("dir", s.st_mode))
         return;

       p = sprintf ("%s%c%s", p, path_get_delimiter (), get_slang_load_path ());
       set_slang_load_path (p);
    }

Notes

Some applications may not support the built-in load-path searching facility provided by the underlying library.

See Also

get_slang_load_path, path_get_delimiter, evalfile, autoload


Next Previous Contents