1 (* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl.ml,v 1.14 2005-01-29 12:22:49 rich Exp $
10 exception Perl_failure of string
12 (* Initialization. This must happen first, otherwise other parts of the
13 * program will segfault because of a missing interpreter.
15 external c_init : unit -> unit = "perl4caml_init"
17 Callback.register_exception "perl4caml_perl_failure" (Perl_failure "");
18 c_init (); (* Initialise C code. *)
21 external int_of_sv : sv -> int = "perl4caml_int_of_sv"
22 external sv_of_int : int -> sv = "perl4caml_sv_of_int"
23 external float_of_sv : sv -> float = "perl4caml_float_of_sv"
24 external sv_of_float : float -> sv = "perl4caml_sv_of_float"
25 external string_of_sv : sv -> string = "perl4caml_string_of_sv"
26 external sv_of_string : string -> sv = "perl4caml_sv_of_string"
27 external sv_is_true : sv -> bool = "perl4caml_sv_is_true"
28 external sv_undef : unit -> sv = "perl4caml_sv_undef"
29 external sv_is_undef : sv -> bool = "perl4caml_sv_is_undef"
30 external sv_yes : unit -> sv = "perl4caml_sv_yes"
31 external sv_no : unit -> sv = "perl4caml_sv_no"
33 let sv_true () = sv_of_int 1
34 let sv_false () = sv_of_int 0
36 let bool_of_sv = sv_is_true
37 let sv_of_bool b = if b then sv_true () else sv_false ()
50 external sv_type : sv -> sv_t = "perl4caml_sv_type"
52 let string_of_sv_t = function
53 SVt_NULL -> "SVt_NULL"
58 | SVt_PVAV -> "SVt_PVAV"
59 | SVt_PVHV -> "SVt_PVHV"
60 | SVt_PVCV -> "SVt_PVCV"
61 | SVt_PVGV -> "SVt_PVGV"
62 | SVt_PVMG -> "SVt_PVMG"
64 external address_of_sv : sv -> Nativeint.t = "perl4caml_address_of_sv"
65 external address_of_av : av -> Nativeint.t = "perl4caml_address_of_av"
66 external address_of_hv : hv -> Nativeint.t = "perl4caml_address_of_hv"
68 external scalarref : sv -> sv = "perl4caml_scalarref"
69 external arrayref : av -> sv = "perl4caml_arrayref"
70 external hashref : hv -> sv = "perl4caml_hashref"
72 external deref : sv -> sv = "perl4caml_deref"
73 external deref_array : sv -> av = "perl4caml_deref_array"
74 external deref_hash : sv -> hv = "perl4caml_deref_hash"
76 external av_empty : unit -> av = "perl4caml_av_empty"
77 external av_of_sv_list : sv list -> av = "perl4caml_av_of_sv_list"
78 external av_push : av -> sv -> unit = "perl4caml_av_push"
79 external av_pop : av -> sv = "perl4caml_av_pop"
80 external av_shift : av -> sv = "perl4caml_av_shift"
81 external av_unshift : av -> sv -> unit = "perl4caml_av_unshift"
82 external av_length : av -> int = "perl4caml_av_length"
83 external av_set : av -> int -> sv -> unit = "perl4caml_av_set"
84 external av_get : av -> int -> sv = "perl4caml_av_get"
85 external av_clear : av -> unit = "perl4caml_av_clear"
86 external av_undef : av -> unit = "perl4caml_av_undef"
87 external av_extend : av -> int -> unit = "perl4caml_av_extend"
91 for i = 0 to av_length av - 1 do
92 list := f (av_get av i) :: !list
98 for i = 0 to av_length av - 1 do
99 list := av_get av i :: !list
103 let av_of_string_list strs =
104 av_of_sv_list (List.map sv_of_string strs)
106 external hv_empty : unit -> hv = "perl4caml_hv_empty"
107 external hv_set : hv -> string -> sv -> unit = "perl4caml_hv_set"
108 external hv_get : hv -> string -> sv = "perl4caml_hv_get"
109 external hv_exists : hv -> string -> bool = "perl4caml_hv_exists"
110 external hv_delete : hv -> string -> unit = "perl4caml_hv_delete"
111 external hv_clear : hv -> unit = "perl4caml_hv_clear"
112 external hv_undef : hv -> unit = "perl4caml_hv_undef"
115 external hv_iterinit : hv -> Int32.t = "perl4caml_hv_iterinit"
116 external hv_iternext : hv -> he = "perl4caml_hv_iternext"
117 external hv_iterkey : he -> string = "perl4caml_hv_iterkey"
118 external hv_iterval : hv -> he -> sv = "perl4caml_hv_iterval"
119 external hv_iternextsv : hv -> string * sv = "perl4caml_hv_iternextsv"
122 let hv = hv_empty () in
123 List.iter (fun (k, v) -> hv_set hv k v) xs;
126 ignore (hv_iterinit hv);
127 (* Someone please rewrite this to make it tail-rec! - Rich. XXX *)
130 let k, v = hv_iternextsv hv in
137 ignore (hv_iterinit hv);
138 (* Someone please rewrite this to make it tail-rec! - Rich. XXX *)
141 let he = hv_iternext hv in
142 let k = hv_iterkey he in
149 ignore (hv_iterinit hv);
150 (* Someone please rewrite this to make it tail-rec! - Rich. XXX *)
153 let he = hv_iternext hv in
154 let v = hv_iterval hv he in
161 external get_sv : ?create:bool -> string -> sv = "perl4caml_get_sv"
162 external get_av : ?create:bool -> string -> av = "perl4caml_get_av"
163 external get_hv : ?create:bool -> string -> hv = "perl4caml_get_hv"
165 external call : ?sv:sv -> ?fn:string -> sv list -> sv
167 external call_array : ?sv:sv -> ?fn:string -> sv list -> sv list
168 = "perl4caml_call_array"
169 external call_void : ?sv:sv -> ?fn:string -> sv list -> unit
170 = "perl4caml_call_void"
172 external eval : string -> sv
175 external call_method : sv -> string -> sv list -> sv
176 = "perl4caml_call_method"
177 external call_method_array : sv -> string -> sv list -> sv list
178 = "perl4caml_call_method_array"
179 external call_method_void : sv -> string -> sv list -> unit
180 = "perl4caml_call_method_void"
181 external call_class_method : string -> string -> sv list -> sv
182 = "perl4caml_call_class_method"
183 external call_class_method_array : string -> string -> sv list -> sv list
184 = "perl4caml_call_class_method_array"
185 external call_class_method_void : string -> string -> sv list -> unit
186 = "perl4caml_call_class_method_void"