sleep.interfaces

Interface Evaluation

public interface Evaluation

A Sleep evaluation is a way to define how a `back quoted` string should work. In Perl any text inside of `back quotes` is fevaluated for embedded $scalar values and then executed as a shell command. The output of the executed command is collected into a perl array and returned as the resulting value of the `back quote` expression.

While executing commands in this way might be a useful abstraction, it seems more fun to allow you, the application developer to define what this syntax should do.

The following is an implementation of perl-like backquote behavior for Sleep:

 import sleep.interfaces.Evaluation;

 import sleep.runtime.Scalar;
 import sleep.runtime.ScriptInstance;
 import sleep.runtime.SleepUtils;

 import java.io.*;

 public class PerlLike implements Evaluation
 {
    public Scalar evaluateString(ScriptInstance script, String value)
    {
       Scalar rv = SleepUtils.getArrayScalar();

       try
       {
          // execute our process and setup a reader for it 
 
          Process proc  = Runtime.getRuntime().exec(value);
          BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

          // read each line from the process output, stuff it into our scalar array rv

          String text = null;
          while ((text = reader.readLine()) != null)
          {
             rv.getArray().push(SleepUtils.getScalar(text));
          }
       }
       catch (IOException ex)
       {
          script.getScriptEnvironment().flagError(ex.toString());
       }

       return rv;
    }
 }

To install the perl-like backquote evaluator into the script environment:

 public boolean scriptLoaded(ScriptInstance script)
 {
    Evaluation perlStuff = new PerlLike();
 
    Hashtable environment = script.getScriptEnvironment().getEnvironment();
    environment.put("%BACKQUOTE%", perlStuff);

    return true;
 }
 

Method Summary

abstract Scalar
evaluateString(ScriptInstance si, String value)
Evaluate the specified string value.

Method Details

evaluateString

public abstract Scalar evaluateString(ScriptInstance si,
                                      String value)
Evaluate the specified string value.
Parameters: