home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Shareware / Comunicatii / advwebrank / awr.msi / disk1.cab / bsh_1.2b6.jar / bsh / commands / eval.bsh < prev    next >
Encoding:
Text File  |  2002-05-26  |  1.5 KB  |  67 lines

  1. /**
  2.     Evaluate the string in the current interpreter (see source()).
  3.     Returns the result of the evaluation or null.
  4.     <p>
  5.  
  6.     Evaluate a string as if it were written directly in the current scope, 
  7.     with side effects in the current scope.
  8.     <p>
  9.     e.g.
  10.     <code><pre>
  11.     a=5;
  12.     eval("b=a*2");
  13.     print(b); // 10
  14.     </pre></code>
  15.     <p>
  16.  
  17.     eval() acts just like invoked text except that any exceptions generated
  18.     by the code are captured in a bsh.EvalError.  This includes ParseException
  19.     for syntactic errors and TargetError for exceptions thrown by the evaluated
  20.     code.
  21.     <p>
  22.     e.g.
  23.     <pre>
  24.     try {
  25.         eval("foo>>><>M>JK$LJLK$");
  26.     } catch ( EvalError e ) {
  27.         // ParseException caught here
  28.     }
  29.  
  30.     try {
  31.         eval("(Integer)true");  // illegal cast
  32.     } catch ( EvalError e ) {
  33.         // TargetException caught here
  34.         print( e.getTarget() )  // prints ClassCastException
  35.     }
  36.     </pre>
  37.     <p>
  38.     
  39.     If you want eval() to throw target exceptions directly, without wrapping
  40.     them, you can simply redefine own eval like so:
  41.  
  42.     <pre>
  43.     myEval( String expression ) {
  44.         try {
  45.             return eval( expression );
  46.         } catch ( TargetError e ) {
  47.             throw e.getTarget();
  48.         }
  49.     }
  50.     </pre>
  51.  
  52.     <p>
  53.     Returns the value of the expression.
  54.     <p>
  55.     Throws bsh.EvalError on error
  56.     <p>
  57.     @return the value of the expression.
  58.     @throws bsh.EvalError on error
  59. */
  60.  
  61. bsh.help.eval = "usage: eval( String expression )";
  62.  
  63. Object eval( String expression ) {
  64.     return this.interpreter.eval( expression, this.caller.namespace );
  65. }
  66.  
  67.