Navigation

 + + + 

Sleep/jIRCii Extensions - Evaluation Bridge


Sleep Evaluation Bridge for Sleep 2.1 Version 2008/6/9         Evaluation Bridge for Sleep 2.0

By default Sleep 2.1 evaluates strings in backquotes(`) like PERL.
The Evaluation Bridge makes it possible for script writers to script backquote handlers.
For example:

import shuteye.Evaluation from: evaluation.jar;
use(^Evaluation);

setEvaluationHandler( { return split(' ', $1); } );

@words = `results in an array containing the blank-separated words`;


Download Version 2008/6/9


Functions Reference

setEvaluationHandler( $function [, $key1 => $value1 [, $key2 => $value2 ...] ] )

sets $function as handler for handling strings in backquotes
where $function is an inline closure (first example),
or a variable that references a closure (regular expression example )
or a function reference (sql example ).

updateEvaluationHandlerVars( $key1 => $newvalue1 [, $key2 => $value2 ...] )

allows the manipulation of the closure variables owned by the handler.


regular expression evaluation

import shuteye.Evaluation from: evaluation.jar;
use(^Evaluation);

$matches = {
  local('$str $regex');
  ($str,$regex) = split('::', $1);
  return matches($str,$regex);
};

setEvaluationHandler( $matches );

$text = '11 12 13 14';

foreach $group (`$text $+ ::(\\d\\d)`) {
  println($group);
}



SQL evaluation

import shuteye.Classloader from: classloader.jar;
import shuteye.Evaluation from: evaluation.jar;

use(^Classloader);
use(^Evaluation);

addClassPath('hsqldb.jar');
addClassPath('slumber.jar');
use(loadClass('no.printf.slumber.JDBC'));

sub sqlEvaluation {
  local('$resultset @result');
  $resultset = dbQuery( $db_con, $1 );
  @result = dbFetchBuffered( $resultset );
  return @result;
}

$connection = dbConnect('org.hsqldb.jdbcDriver', 'jdbc:hsqldb:presidents', 'SA', '');

if ( checkError($err) ) {
  println('error connecting db: ' . $err);
}

setEvaluationHandler(&sqlEvaluation , $db_con => $connection );
  
# sql statement in backquotes evaluates to an array containing the row hashes
foreach %row (`SELECT * FROM PRESIDENTS`) { 
  println( %row['ID'] . '. ' . "\t" . %row['NAME'] . "\t" . 
               %row['TERMBEGIN'] . '-' . %row['TERMEND'] );
}

# do not keep a reference to $connection in the handler
updateEvaluationHandlerVars( $db_con => $null );

dbClose( $connection );


Sleep Evaluation Bridge for Sleep 2.0 Version 2006/02/22         Evaluation Bridge for Sleep 2.1

By default Sleep evaluates strings in backquotes(`) like strings in double quotes(").
The EvaluationBridge makes it possible for script writers to script backquote handlers.
For example:

use('evaluation.jar','org.ululatus.sleep.bridges.EvaluationBridge');

setEvaluationHandler( { return split(' ', $1); } );

@words = `results in an array containing the blank-separated words`;


Download Version 2006/02/22


Functions Reference

setEvaluationHandler( $function [, $key1 => $value1 [, $key2 => $value2 ...] ] )

sets $function as handler for handling strings in backquotes
where $function is an inline closure (first example),
or a variable that references a closure (perl-like example )
or a function reference (sql example ).

updateEvaluationHandlerVars( $key1 => $newvalue1 [, $key2 => $value2 ...] )

allows the manipulation of the closure variables owned by the handler.


Perl-like evaluation

use('evaluation.jar','org.ululatus.sleep.bridges.EvaluationBridge');

$perl_like = {
  local('$proc $reader @output $line $err');
  @output = array();
  
  $proc = [[java.lang.Runtime getRuntime] exec: $1];
  
  if ( checkError($err) || !-istrue $proc ) {
    println( 'error in evaluation handler:' . $err);
    return @output;
  }

  $reader = [new java.io.BufferedReader: [new java.io.InputStreamReader: [$proc getInputStream]]];

  if ( checkError($err) ) {
    println( 'error in evaluation handler:' . $err);
    [$proc destroy];
    return @output;
  }

  # read each line from the process output, stuff it into our array

  $line = [$reader readLine];
  if ( checkError($err) ) {
    println( 'error in evaluation handler:' . $err);
    [$reader close];
    [$proc destroy];
    return @output;
  }
  else {
    push( @output, '' . $line, size(@output));
    $available = 1;
    while ( -istrue $available )
    {
      $line = [$reader readLine];
      push( @output, '' . $line, size(@output));
      if ( checkError($err) ) {
        println( 'error in evaluation handler:' . $err);
        [$reader close];
        [$proc destroy];
        return @output;
      }
      [$reader mark: 2];
      if ( [$reader read] != -1 ) {
        [$reader reset];
      }
      else {
        $available = 0;
      }
    }
  }
  [$reader close];
  return @output;
};

setEvaluationHandler($perl_like);

if ( -isDir @ARGV[0] ) {
  $dir = @ARGV[0];
}
else {
  $dir = '.';
}

@result = `cmd.exe /c dir $dir`;
# comment line above and uncomment next line if you are using linux or cygwin
# @result = `ls -l $dir`;

foreach $result (@result) {
  println($result);
}



SQL evaluation

use('classloader.jar','org.ululatus.sleep.bridges.ClassloaderBridge');
use('evaluation.jar','org.ululatus.sleep.bridges.EvaluationBridge');

addClassPath('hsqldb.jar');
addClassPath('JDBC.jar');
useClass('JDBC');

sub sqlEvaluation {
  local('$resultset @result');
  $resultset = dbQuery( $db_con, $1 );
  @result = dbFetchBuffered( $resultset );
  return @result;
}

$connection = dbConnect('org.hsqldb.jdbcDriver', 'jdbc:hsqldb:presidents', 'SA', '');

if ( checkError($err) ) {
  println('error connecting db: ' . $err);
}

setEvaluationHandler(&sqlEvaluation , $db_con => $connection );
  
# sql statement in backquotes evaluates to an array containing the row hashes
foreach %row (`SELECT * FROM PRESIDENTS`) { 
  println( %row['ID'] . '. ' . "\t" . %row['NAME'] . "\t" . 
               %row['TERMBEGIN'] . '-' . %row['TERMEND'] );
}

# do not keep a reference to $connection in the handler
updateEvaluationHandlerVars( $db_con => $null );

dbClose( $connection );

                                                                                                                                                                                                                           

ululatus.org     Valid XHTML 1.0!      Valid CSS!      Last modified 25 Jun 2008 11:20