ef8dea0e958a63d0205eb9277e730b2c96e0575d
[perl4caml.git] / perl.mli
1 (** Interface to Perl from OCaml.
2   *
3   * Copyright (C) 2003 Merjis Ltd.
4   *
5   * $Id: perl.mli,v 1.9 2003-10-18 12:36:09 rich Exp $
6   *)
7
8 type t
9 (** Perl interpreter (abstract type). *)
10
11 type sv
12 (** Perl scalar value. *)
13
14 type av
15 (** Perl array value. *)
16
17 type hv
18 (** Perl hash value. *)
19
20 exception Perl_failure of string
21 (** [die] in Perl code is translated automatically into this exception. *)
22
23 val current_interpreter : unit -> t
24 (** The [Perl] module has a notion of the "current" interpreter. Throws
25   * [Not_found] if there is no current interpreter.
26   *
27   * When a program starts up, if it has been linked with [perl_init.cmo]
28   * (which is should be), an interpreter is created for you. Normally
29   * this should be all you need to know about interpreters, unless you
30   * want to be really good and call
31   * [Perl.destroy (Perl.current_interpreter ())] at the end of your
32   * program to do proper cleanup.
33   *
34   * You can also, under certain circumstances, create other interpreters,
35   * although this is experiemental and definitely not recommended.
36   *
37   * If Perl was compiled with [-Dusemultiplicity] then you can create
38   * mutliple interpreters at the same time and switch between them by
39   * calling {!Perl.set_context}.
40   *
41   * Otherwise you may destroy the current interpreter and create another
42   * one (provided that at no time you have two "live" interpreters),
43   * by calling {!Perl.destroy} followed by {!Perl.create}.
44 *)
45
46 val destroy : t -> unit
47 (** Destroy the Perl interpreter, performing any necessary cleanup.
48   *
49   * You should call [Perl.destroy (Perl.current_interpreter ())]  at
50   * the end of your program, otherwise Perl won't properly clean up
51   * (running [END] blocks, destroying objects and the like).
52   *
53   * Note that a Perl interpreter is created for you by default when you
54   * use perl4caml.
55   *
56   * The current interpreter can be found by calling
57   * {!Perl.current_interpreter}.
58   *)
59
60 val create : ?args:string array -> unit -> t
61 (** Create a new Perl interpreter. (Note that a Perl interpreter is created
62   * for you by default so you don't need to call this).
63   *
64   * The optional [?args] parameter is the command line passed to the
65   * interpreter, and controls things like whether warnings are enabled
66   * ([-w]) and which file(s) are parsed. The first element in the
67   * array is the executable name (you can just set this to [""]).
68   *
69   * Perl won't allow you to create multiple interpreters at the same time
70   * unless Perl itself was compiled with [-Dusemultiplicity]. However you
71   * can create, then destroy, then create another and so on.
72   *
73   * The newly created interpreter is set as the "current interpreter".
74   *)
75
76 val set_context : t -> unit
77 (** IF Perl was compiled with [-Dusemultiplicity] and IF you are using
78   * multiple interpreters at the same time, then you must call this to
79   * set the implied "current" interpreter.
80   *
81   * Most users will never need to call this function.
82   *)
83
84 val int_of_sv : sv -> int
85 (** Convert a Perl [SV] into an integer. Note that OCaml [int]s aren't
86   * large enough to store the full 32 (or 64) bits from a Perl integer,
87   * so you may get a silent overflow.
88   *)
89 val sv_of_int : int -> sv
90 (** Convert an [int] into a Perl [SV]. *)
91 val float_of_sv : sv -> int
92 (** Convert a Perl [SV] into a float. *)
93 val sv_of_float : int -> sv
94 (** Convert a [float] into a Perl [SV]. *)
95 val string_of_sv : sv -> string
96 (** Convert a Perl [SV] into a string. *)
97 val sv_of_string : string -> sv
98 (** Convert a [string] into a Perl [SV]. *)
99 val bool_of_sv : sv -> bool
100 (** Convert an [SV] into a boolean. *)
101 val sv_of_bool : bool -> sv
102 (** Convert a boolean into an [SV]. *)
103
104 val sv_is_true : sv -> bool
105 (** Return [true] if the [SV] is "true" (in the Perl sense of truth). *)
106 val sv_is_undef : sv -> bool
107 (** Return [true] if the [SV] is undefined (is [undef]). *)
108 val sv_undef : unit -> sv
109 (** Returns [undef]. *)
110 val sv_true : unit -> sv
111 (** Returns an [SV] which is true. *)
112 val sv_false : unit -> sv
113 (** Returns an [SV] which is false. *)
114 val sv_yes : unit -> sv
115 (** Returns Perl's internal [PL_sv_yes]. (There are some unresolved issues
116   * with using this, so use {!Perl.sv_true} instead). *)
117 val sv_no : unit -> sv
118 (** Returns Perl's internal [PL_sv_no]. (There are some unresolved issues
119   * with using this, so use {!Perl.sv_false} instead). *)
120
121 (* Actually there are many more types defined than this ... *)
122 type sv_t    = SVt_NULL
123              | SVt_IV        (** Integer scalar.  *)
124              | SVt_NV        (** Floating point scalar. *)
125              | SVt_PV        (** String scalar. *)
126              | SVt_RV        (** Reference. *)
127              | SVt_PVAV      (** Array ref. *)
128              | SVt_PVHV      (** Hash ref. *)
129              | SVt_PVCV      (** Code ref. *)
130              | SVt_PVGV      (** Glob. *)
131              | SVt_PVMG      (** Blessed or magical scalar. *)
132 val sv_type : sv -> sv_t
133 (** Return the type of data contained in an [SV]. Somewhat equivalent to
134   * calling Perl's [ref] function.
135   *)
136 val string_of_sv_t : sv_t -> string
137 (** Return a printable string for an [sv_t] ([SV] type). *)
138
139 val deref : sv -> sv
140 (** The input is a reference to a scalar. This returns the underlying
141   * scalar [SV]. If the input is not a reference to a scalar, throws
142   * [Invalid_arg].
143   *)
144 val deref_array : sv -> av
145 (** The input is a reference to an array. This returns the underlying
146   * array [AV]. If the input is not a reference to an array, throws
147   * [Invalid_arg].
148   *)
149 val deref_hash : sv -> hv
150 (** The input is a reference to a hash. This returns the underlying
151   * hash [HV]. If the input is not a reference to a hash, throws
152   * [Invalid_arg].
153   *)
154
155 val av_empty : unit -> av
156 (** Create an empty [AV] (array). *)
157 val av_of_sv_list : sv list -> av
158 (** Create an array from a list of [SVs]. *)
159 val av_push : av -> sv -> unit
160 (** Append the [SV] to the end of the array. Same as Perl
161   * [push \@av, $sv]. *)
162 val av_pop : av -> sv
163 (** Remove the [SV] at the end of the array and return it. Same as
164   * Perl [$sv = pop \@av]. *)
165 val av_shift : av -> sv
166 (** Remove the [SV] at the beginning of the array and return it. Same as
167   * Perl [$sv = shift \@av]. *)
168 val av_unshift : av -> sv -> unit
169 (** Prepend the [SV] to the start of the array. Same as Perl
170   * [unshift \@av, $sv]. *)
171 val av_length : av -> int
172 (** Return the length of the [AV]. *)
173 val av_set : av -> int -> sv -> unit
174 (** Replace the i'th element of the [AV] with [SV]. *)
175 val av_get : av -> int -> sv
176 (** Get the i'th element of the [AV]. *)
177 val av_clear : av -> unit
178 (** Remove all elements from the [AV]. Same as Perl [\@av = ()]. *)
179 val av_undef : av -> unit
180 (** Delete the [AV] (and all elements in it). Same as Perl [undef \@av]. *)
181 val av_extend : av -> int -> unit
182 (** Extend the [AV] so it contains at least [n+1] elements. *)
183 val av_map : (sv -> 'a) -> av -> 'a list
184 (** Map a function over the elements in the [AV], return a list of the
185   * results. *)
186 val list_of_av : av -> sv list
187 (** Convert an [AV] into a simple list of [SV]s. *)
188 val av_of_string_list : string list -> av
189 (** Build an [AV] from a list of strings. *)
190
191 val hv_empty : unit -> hv
192 (** Create an empty [HV] (hash). *)
193 val hv_set : hv -> string -> sv -> unit
194 (** Store the given [SV] in the named key in the hash. *)
195 val hv_get : hv -> string -> sv
196 (** Return the [SV] at the key in the hash. Throws [Not_found] if no key. *)
197 val hv_exists : hv -> string -> bool
198 (** Return true if the hash contains the given key. Same as Perl [exists]. *)
199 val hv_delete : hv -> string -> unit
200 (** Delete the given key from the hash. Same as Perl [delete]. *)
201 val hv_clear : hv -> unit
202 (** Remove all elements from the [HV]. Same as Perl [%av = ()]. *)
203 val hv_undef : hv -> unit
204 (** Delete the [HV] (and all elements in it). Same as Perl [undef %hv]. *)
205
206 val get_sv : ?create:bool -> string -> sv
207 (** Return a scalar value by name. For example, if you have a symbol
208   * called [$a] in Perl, then [get_sv "a"] will return its value.
209   *
210   * If the symbol does not exist, this throws [Not_found].
211   *
212   * If the optional [?create] argument is set to true and the symbol does
213   * not exist, then Perl will create the symbol (with value [undef]) and
214   * this function will return the [SV] for [undef].
215   *)
216 val get_av : ?create:bool -> string -> av
217 (** Same as {!Perl.get_sv} except will return and/or create [\@a]. *)
218 val get_hv : ?create:bool -> string -> hv
219 (** Same as {!Perl.get_sv} except will return and/or create [%a]. *)
220
221 val call : ?sv:sv -> ?fn:string -> sv list -> sv
222 (** Call a Perl function in a scalar context, either by name (using the [?fn]
223   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
224   *
225   * Returns the Perl [SV] containing the result value. (See
226   * {!Perl.int_of_sv} etc.).
227   *
228   * If the Perl code calls [die] then this will throw [Perl_failure].
229   *)
230
231 val call_array : ?sv:sv -> ?fn:string -> sv list -> sv list
232 (** Call a Perl function in an array context, either by name (using the [?fn]
233   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
234   *
235   * Returns the list of results.
236   *
237   * If the Perl code calls [die] then this will throw [Perl_failure].
238   *)
239
240 val call_void : ?sv:sv -> ?fn:string -> sv list -> unit
241 (** Call a Perl function in a void context, either by name (using the [?fn]
242   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
243   *
244   * Any results are discarded.
245   *
246   * If the Perl code calls [die] then this will throw [Perl_failure].
247   *)
248
249 val eval : string -> sv
250 (** This is exactly like the Perl [eval] command. It evaluates a piece of
251   * Perl code (in scalar context) and returns the result (a Perl [SV]).
252   *)
253
254 val call_method : sv -> string -> sv list -> sv
255 (** [call_method obj name [parameters]] calls the method [name] on the Perl
256   * object [obj] with the given parameters, in a scalar context. Thus this
257   * is equivalent to [$obj->name (parameters)].
258   *
259   * Returns the Perl [SV] containing the result value.
260   *
261   * If the method calls [die] then this will throw [Perl_failure].
262   *)
263
264 val call_method_array : sv -> string -> sv list -> sv list
265 (** Like [call_method], but the method is called in an array context. *)
266
267 val call_method_void : sv -> string -> sv list -> unit
268 (** Like [call_method], but the method is called in a void context (results
269   * are discarded). *)
270
271 val call_class_method : string -> string -> sv list -> sv
272 (** [call_class_method classname name [parameters]] calls the static method
273   * [name] in the Perl class [classname] with the given parameters, in a
274   * scalar context. Thus this is equivalent to [$classname->name (parameters)].
275   *
276   * Returns the Perl [SV] containing the result value.
277   *
278   * If the static method calls [die] then this will throw [Perl_failure].
279   *)
280
281 val call_class_method_array : string -> string -> sv list -> sv list
282 (** Like [call_class_method], but the method is called in an array context. *)
283
284 val call_class_method_void : string -> string -> sv list -> unit
285 (** Like [call_class_method], but the method is called in a void context. *)