Handle NUL characters in Perl strings properly.
[perl4caml.git] / perl.mli
1 (** Interface to Perl from OCaml.
2   *
3   * Copyright (C) 2003 Merjis Ltd.
4   *
5   * $Id: perl.mli,v 1.14 2005-01-29 12:22:49 rich Exp $
6   *)
7
8 type sv
9 (** Perl scalar value. *)
10
11 type av
12 (** Perl array value. *)
13
14 type hv
15 (** Perl hash value. *)
16
17 exception Perl_failure of string
18 (** [die] in Perl code is translated automatically into this exception. *)
19
20 val int_of_sv : sv -> int
21 (** Convert a Perl [SV] into an integer. Note that OCaml [int]s aren't
22   * large enough to store the full 32 (or 64) bits from a Perl integer,
23   * so you may get a silent overflow.
24   *)
25 val sv_of_int : int -> sv
26 (** Convert an [int] into a Perl [SV]. *)
27 val float_of_sv : sv -> float
28 (** Convert a Perl [SV] into a float. *)
29 val sv_of_float : float -> sv
30 (** Convert a [float] into a Perl [SV]. *)
31 val string_of_sv : sv -> string
32 (** Convert a Perl [SV] into a string. *)
33 val sv_of_string : string -> sv
34 (** Convert a [string] into a Perl [SV]. *)
35 val bool_of_sv : sv -> bool
36 (** Convert an [SV] into a boolean. *)
37 val sv_of_bool : bool -> sv
38 (** Convert a boolean into an [SV]. *)
39
40 val sv_is_true : sv -> bool
41 (** Return [true] if the [SV] is "true" (in the Perl sense of truth). *)
42 val sv_is_undef : sv -> bool
43 (** Return [true] if the [SV] is undefined (is [undef]). *)
44 val sv_undef : unit -> sv
45 (** Returns [undef]. *)
46 val sv_true : unit -> sv
47 (** Returns an [SV] which is true. *)
48 val sv_false : unit -> sv
49 (** Returns an [SV] which is false. *)
50 val sv_yes : unit -> sv
51 (** Returns Perl's internal [PL_sv_yes]. (There are some unresolved issues
52   * with using this, so use {!Perl.sv_true} instead). *)
53 val sv_no : unit -> sv
54 (** Returns Perl's internal [PL_sv_no]. (There are some unresolved issues
55   * with using this, so use {!Perl.sv_false} instead). *)
56
57 (* Actually there are many more types defined than this ... *)
58 type sv_t    = SVt_NULL
59              | SVt_IV        (** Integer scalar.  *)
60              | SVt_NV        (** Floating point scalar. *)
61              | SVt_PV        (** String scalar. *)
62              | SVt_RV        (** Reference. *)
63              | SVt_PVAV      (** Array. *)
64              | SVt_PVHV      (** Hash. *)
65              | SVt_PVCV      (** Code. *)
66              | SVt_PVGV      (** Glob (possibly a file handle). *)
67              | SVt_PVMG      (** Blessed or magical scalar. *)
68 val sv_type : sv -> sv_t
69 (** Return the type of data contained in an [SV]. Somewhat equivalent to
70   * calling Perl's [ref] function.
71   *)
72 val string_of_sv_t : sv_t -> string
73 (** Return a printable string for an [sv_t] ([SV] type). *)
74
75 val address_of_sv : sv -> Nativeint.t
76 (** Returns the address of the SV.  Useful for debugging since
77   * Perl also prints out addresses on internal errors.
78   *)
79 val address_of_av : av -> Nativeint.t
80 (** Returns the address of the AV.  Useful for debugging since
81   * Perl also prints out addresses on internal errors.
82   *)
83 val address_of_hv : hv -> Nativeint.t
84 (** Returns the address of the HV.  Useful for debugging since
85   * Perl also prints out addresses on internal errors.
86   *)
87
88 val scalarref : sv -> sv
89 (** Given a scalar, this returns a reference to the scalar. Note that
90   * because references are [SV]s, this returns [sv].
91   *)
92 val arrayref : av -> sv
93 (** Given an array, this returns a reference to the array. Note that
94   * because references are [SV]s, this returns [sv].
95   *)
96 val hashref : hv -> sv
97 (** Given a hash, this returns a reference to the hash. Note that
98   * because references are [SV]s, this returns [sv].
99   *)
100
101 val deref : sv -> sv
102 (** The input is a reference to a scalar. This returns the underlying
103   * scalar [SV]. If the input is not a reference to a scalar, throws
104   * [Invalid_argument].
105   *)
106 val deref_array : sv -> av
107 (** The input is a reference to an array. This returns the underlying
108   * array [AV]. If the input is not a reference to an array, throws
109   * [Invalid_argument].
110   *)
111 val deref_hash : sv -> hv
112 (** The input is a reference to a hash. This returns the underlying
113   * hash [HV]. If the input is not a reference to a hash, throws
114   * [Invalid_argument].
115   *)
116
117 val av_empty : unit -> av
118 (** Create an empty [AV] (array). *)
119 val av_of_sv_list : sv list -> av
120 (** Create an array from a list of [SVs]. *)
121 val av_push : av -> sv -> unit
122 (** Append the [SV] to the end of the array. Same as Perl
123   * [push \@av, $sv]. *)
124 val av_pop : av -> sv
125 (** Remove the [SV] at the end of the array and return it. Same as
126   * Perl [$sv = pop \@av]. *)
127 val av_shift : av -> sv
128 (** Remove the [SV] at the beginning of the array and return it. Same as
129   * Perl [$sv = shift \@av]. *)
130 val av_unshift : av -> sv -> unit
131 (** Prepend the [SV] to the start of the array. Same as Perl
132   * [unshift \@av, $sv]. *)
133 val av_length : av -> int
134 (** Return the length of the [AV]. *)
135 val av_set : av -> int -> sv -> unit
136 (** Replace the i'th element of the [AV] with [SV]. *)
137 val av_get : av -> int -> sv
138 (** Get the i'th element of the [AV]. *)
139 val av_clear : av -> unit
140 (** Remove all elements from the [AV]. Same as Perl [\@av = ()]. *)
141 val av_undef : av -> unit
142 (** Delete the [AV] (and all elements in it). Same as Perl [undef \@av]. *)
143 val av_extend : av -> int -> unit
144 (** Extend the [AV] so it contains at least [n+1] elements.  Note that
145   * this apparently just changes the amount of allocated storage.  The
146   * extra elements are not visible until you store something in them.
147   *)
148 val av_map : (sv -> 'a) -> av -> 'a list
149 (** Map a function over the elements in the [AV], return a list of the
150   * results. *)
151 val list_of_av : av -> sv list
152 (** Convert an [AV] into a simple list of [SV]s. *)
153 val av_of_string_list : string list -> av
154 (** Build an [AV] from a list of strings. *)
155
156 val hv_empty : unit -> hv
157 (** Create an empty [HV] (hash). *)
158 val hv_set : hv -> string -> sv -> unit
159 (** Store the given [SV] in the named key in the hash. *)
160 val hv_get : hv -> string -> sv
161 (** Return the [SV] at the key in the hash. Throws [Not_found] if no key. *)
162 val hv_exists : hv -> string -> bool
163 (** Return true if the hash contains the given key. Same as Perl [exists]. *)
164 val hv_delete : hv -> string -> unit
165 (** Delete the given key from the hash. Same as Perl [delete]. *)
166 val hv_clear : hv -> unit
167 (** Remove all elements from the [HV]. Same as Perl [%av = ()]. *)
168 val hv_undef : hv -> unit
169 (** Delete the [HV] (and all elements in it). Same as Perl [undef %hv]. *)
170 val hv_of_assoc : (string * sv) list -> hv
171 (** Create an [HV] directly from an assoc list.  Perl hashes cannot
172   * support multiple values attached to the same key, so if you try
173   * to provide an assoc list with multiple identical keys, the results
174   * will be undefined.
175   *)
176 val assoc_of_hv : hv -> (string * sv) list
177 (** Take an [HV] and return an assoc list. *)
178 val hv_keys : hv -> string list
179 (** Return all the keys of an [HV]. *)
180 val hv_values : hv -> sv list
181 (** Return all the values of an [HV]. *)
182
183 (* The following are the low-level iteration interface to hashes,
184  * which you probably shouldn't use directly.  Use {!hv_keys},
185  * {!assoc_of_hv}, etc. instead.  See [perlguts(3)] if you really
186  * want to use this interface.
187  *)
188 type he
189 val hv_iterinit : hv -> Int32.t
190 val hv_iternext : hv -> he
191 val hv_iterkey : he -> string
192 val hv_iterval : hv -> he -> sv
193 val hv_iternextsv : hv -> string * sv
194
195 val get_sv : ?create:bool -> string -> sv
196   (** Return a scalar value by name. For example, if you have a symbol
197     * called [$a] in Perl, then [get_sv "a"] will return its value.
198     *
199     * If the symbol does not exist, this throws [Not_found].
200     *
201     * If the optional [?create] argument is set to true and the symbol does
202     * not exist, then Perl will create the symbol (with value [undef]) and
203     * this function will return the [SV] for [undef].
204   *)
205 val get_av : ?create:bool -> string -> av
206 (** Same as {!Perl.get_sv} except will return and/or create [\@a]. *)
207 val get_hv : ?create:bool -> string -> hv
208 (** Same as {!Perl.get_sv} except will return and/or create [%a]. *)
209
210 val call : ?sv:sv -> ?fn:string -> sv list -> sv
211 (** Call a Perl function in a scalar context, either by name (using the [?fn]
212   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
213   *
214   * Returns the Perl [SV] containing the result value. (See
215   * {!Perl.int_of_sv} etc.).
216   *
217   * If the Perl code calls [die] then this will throw [Perl_failure].
218   *)
219
220 val call_array : ?sv:sv -> ?fn:string -> sv list -> sv list
221 (** Call a Perl function in an array context, either by name (using the [?fn]
222   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
223   *
224   * Returns the list of results.
225   *
226   * If the Perl code calls [die] then this will throw [Perl_failure].
227   *)
228
229 val call_void : ?sv:sv -> ?fn:string -> sv list -> unit
230 (** Call a Perl function in a void context, either by name (using the [?fn]
231   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
232   *
233   * Any results are discarded.
234   *
235   * If the Perl code calls [die] then this will throw [Perl_failure].
236   *)
237
238 val eval : string -> sv
239 (** This is exactly like the Perl [eval] command. It evaluates a piece of
240   * Perl code (in scalar context) and returns the result (a Perl [SV]).
241   *)
242
243 val call_method : sv -> string -> sv list -> sv
244 (** [call_method obj name [parameters]] calls the method [name] on the Perl
245   * object [obj] with the given parameters, in a scalar context. Thus this
246   * is equivalent to [$obj->name (parameters)].
247   *
248   * Returns the Perl [SV] containing the result value.
249   *
250   * If the method calls [die] then this will throw [Perl_failure].
251   *)
252
253 val call_method_array : sv -> string -> sv list -> sv list
254 (** Like [call_method], but the method is called in an array context. *)
255
256 val call_method_void : sv -> string -> sv list -> unit
257 (** Like [call_method], but the method is called in a void context (results
258   * are discarded). *)
259
260 val call_class_method : string -> string -> sv list -> sv
261 (** [call_class_method classname name [parameters]] calls the static method
262   * [name] in the Perl class [classname] with the given parameters, in a
263   * scalar context. Thus this is equivalent to [$classname->name (parameters)].
264   *
265   * Returns the Perl [SV] containing the result value.
266   *
267   * If the static method calls [die] then this will throw [Perl_failure].
268   *)
269
270 val call_class_method_array : string -> string -> sv list -> sv list
271 (** Like [call_class_method], but the method is called in an array context. *)
272
273 val call_class_method_void : string -> string -> sv list -> unit
274 (** Like [call_class_method], but the method is called in a void context. *)