Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] perl4caml (Call Perl code and functions from Objective Caml)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: [Caml-list] perl4caml (Call Perl code and functions from Objective Caml)
Perhaps controversially I've written a prototype, pre-alpha interface
for calling Perl code from OCaml. You can download it here:

The long term aim here is to be able to utilise the huge resource that
is CPAN ( from within OCaml. It wouldn't be
completely automatic because of course you would still need to write
interface specifications (similar in concept to .mli files) to make
things type-safe.

A wrapped Perl library would never be as fast or efficient as a native
OCaml implementation, but at least the option would be there.


Here is an example:


sub return1
    print "this is the 'return1' function!\n";

sub adder
    $_[0] + $_[1]


open Printf

let () =
  (* Arguments passed to the Perl "command line". Loads [] *)
  let args = [| ""; "-wT"; "" |] in

  (* Create the Perl interpreter. *)
  let pl = Perl.create ~args () in

  (* Call some subroutines in []. *)
  let sv = Perl.call_scalar "return1" [] in
  printf "return1 = %d\n" (Perl.int_of_sv sv);

  let sv = Perl.call_scalar "adder" [`Int 3; `Int 4] in
  printf "adder (3, 4) = %d\n" (Perl.int_of_sv sv);

  (* Evaluate a simple expression. *)
  Perl.eval "$a = 3";
  printf "$a contains %d\n" (Perl.int_of_sv (Perl.get_sv "a"));

  (* Destroy the interpreter. *)
  Perl.destroy pl


Richard Jones.
Merjis Ltd. - all your business data are belong to you.
MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
RPMs, pkgs etc. Linux, BSD, Solaris.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: