[plt-scheme] Perl and MzScheme

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jun 14 06:45:28 EDT 2004

BTW, here's a very quick hack using the foreign thing that adds a few
simple Perl bindings.  Just in case anyone wants to play with it.

(BTW#2, so I've had a brief look at how to do such things with Perl.
 From now on I'll be getting violent if anyone says that plt doesn't
install as nicely as perl, or that it is complex to compile/link plt
code compared to perl.)

(require (lib "foreign.ss"))
(define libperl
  (ffi-lib "/usr/lib/perl5/5.8.3/i386-linux-thread-multi/CORE/libperl.so"))
(define interp ((get-ffi-obj "perl_alloc" libperl (_fun -> _pointer))))
((get-ffi-obj "perl_construct" libperl (_fun _pointer -> _void)) interp)
((get-ffi-obj "perl_parse" libperl
              (_fun _pointer _pointer _int (_list i _string) _pointer -> _void))
 interp #f 3 '("" "-e" "0") #f)
((get-ffi-obj "perl_run" libperl (_fun _pointer -> _int)) interp)
(define perl-eval
  (get-ffi-obj "Perl_eval_pv" libperl
               (_fun _pointer _string (_bool = #t) -> _pointer)))
(define perl->bytes
  (get-ffi-obj "Perl_sv_2pvutf8" libperl
               (_fun _pointer _pointer (len : (_ptr o _int))
                     -> (_bytes o len))))
(define perl-call
  (get-ffi-obj "Perl_call_sv" libperl
               (_fun _pointer _pointer _int -> _int)))
(define (perl-eval/string str)
  (bytes->string/utf-8 (perl->bytes interp (perl-eval interp str))))
(define (perl-eval/proc str)
  (let ([pp (perl-eval interp str)])
    (lambda () (perl-call interp pp 0))))
(perl-eval/string "$a = 3; $a **= 2")
(perl-eval/proc "sub { print \"FOO\\n\"; }")

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.