Added submit_form method.
[perl4caml.git] / perl.mli
1 (** Interface to Perl from OCaml.
2   *
3   * Copyright (C) 2003 Merjis Ltd.
4   *
5   * $Id: perl.mli,v 1.15 2005-04-14 13:05:12 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 reftype : sv -> sv_t
76 (** The parameter [sv] must be a reference.  This convenience function
77  * works out what it is a reference to, either a scalar, array, hash,
78  * code or glob.  If the parameter is not a reference, or is a reference
79  * to an unknown type, then this will throw [Invalid_argument].  *)
80
81 val address_of_sv : sv -> Nativeint.t
82 (** Returns the address of the SV.  Useful for debugging since
83   * Perl also prints out addresses on internal errors.
84   *)
85 val address_of_av : av -> Nativeint.t
86 (** Returns the address of the AV.  Useful for debugging since
87   * Perl also prints out addresses on internal errors.
88   *)
89 val address_of_hv : hv -> Nativeint.t
90 (** Returns the address of the HV.  Useful for debugging since
91   * Perl also prints out addresses on internal errors.
92   *)
93
94 val scalarref : sv -> sv
95 (** Given a scalar, this returns a reference to the scalar. Note that
96   * because references are [SV]s, this returns [sv].
97   *)
98 val arrayref : av -> sv
99 (** Given an array, this returns a reference to the array. Note that
100   * because references are [SV]s, this returns [sv].
101   *)
102 val hashref : hv -> sv
103 (** Given a hash, this returns a reference to the hash. Note that
104   * because references are [SV]s, this returns [sv].
105   *)
106
107 val deref : sv -> sv
108 (** The input is a reference to a scalar. This returns the underlying
109   * scalar [SV]. If the input is not a reference to a scalar, throws
110   * [Invalid_argument].
111   *)
112 val deref_array : sv -> av
113 (** The input is a reference to an array. This returns the underlying
114   * array [AV]. If the input is not a reference to an array, throws
115   * [Invalid_argument].
116   *)
117 val deref_hash : sv -> hv
118 (** The input is a reference to a hash. This returns the underlying
119   * hash [HV]. If the input is not a reference to a hash, throws
120   * [Invalid_argument].
121   *)
122
123 val av_empty : unit -> av
124 (** Create an empty [AV] (array). *)
125 val av_of_sv_list : sv list -> av
126 (** Create an array from a list of [SVs]. *)
127 val av_push : av -> sv -> unit
128 (** Append the [SV] to the end of the array. Same as Perl
129   * [push \@av, $sv]. *)
130 val av_pop : av -> sv
131 (** Remove the [SV] at the end of the array and return it. Same as
132   * Perl [$sv = pop \@av]. *)
133 val av_shift : av -> sv
134 (** Remove the [SV] at the beginning of the array and return it. Same as
135   * Perl [$sv = shift \@av]. *)
136 val av_unshift : av -> sv -> unit
137 (** Prepend the [SV] to the start of the array. Same as Perl
138   * [unshift \@av, $sv]. *)
139 val av_length : av -> int
140 (** Return the length of the [AV]. *)
141 val av_set : av -> int -> sv -> unit
142 (** Replace the i'th element of the [AV] with [SV]. *)
143 val av_get : av -> int -> sv
144 (** Get the i'th element of the [AV]. *)
145 val av_clear : av -> unit
146 (** Remove all elements from the [AV]. Same as Perl [\@av = ()]. *)
147 val av_undef : av -> unit
148 (** Delete the [AV] (and all elements in it). Same as Perl [undef \@av]. *)
149 val av_extend : av -> int -> unit
150 (** Extend the [AV] so it contains at least [n+1] elements.  Note that
151   * this apparently just changes the amount of allocated storage.  The
152   * extra elements are not visible until you store something in them.
153   *)
154 val av_map : (sv -> 'a) -> av -> 'a list
155 (** Map a function over the elements in the [AV], return a list of the
156   * results. *)
157 val list_of_av : av -> sv list
158 (** Convert an [AV] into a simple list of [SV]s. *)
159 val av_of_string_list : string list -> av
160 (** Build an [AV] from a list of strings. *)
161
162 val hv_empty : unit -> hv
163 (** Create an empty [HV] (hash). *)
164 val hv_set : hv -> string -> sv -> unit
165 (** Store the given [SV] in the named key in the hash. *)
166 val hv_get : hv -> string -> sv
167 (** Return the [SV] at the key in the hash. Throws [Not_found] if no key. *)
168 val hv_exists : hv -> string -> bool
169 (** Return true if the hash contains the given key. Same as Perl [exists]. *)
170 val hv_delete : hv -> string -> unit
171 (** Delete the given key from the hash. Same as Perl [delete]. *)
172 val hv_clear : hv -> unit
173 (** Remove all elements from the [HV]. Same as Perl [%av = ()]. *)
174 val hv_undef : hv -> unit
175 (** Delete the [HV] (and all elements in it). Same as Perl [undef %hv]. *)
176 val hv_of_assoc : (string * sv) list -> hv
177 (** Create an [HV] directly from an assoc list.  Perl hashes cannot
178   * support multiple values attached to the same key, so if you try
179   * to provide an assoc list with multiple identical keys, the results
180   * will be undefined.
181   *)
182 val assoc_of_hv : hv -> (string * sv) list
183 (** Take an [HV] and return an assoc list. *)
184 val hv_keys : hv -> string list
185 (** Return all the keys of an [HV]. *)
186 val hv_values : hv -> sv list
187 (** Return all the values of an [HV]. *)
188
189 (* The following are the low-level iteration interface to hashes,
190  * which you probably shouldn't use directly.  Use {!hv_keys},
191  * {!assoc_of_hv}, etc. instead.  See [perlguts(3)] if you really
192  * want to use this interface.
193  *)
194 type he
195 val hv_iterinit : hv -> Int32.t
196 val hv_iternext : hv -> he
197 val hv_iterkey : he -> string
198 val hv_iterval : hv -> he -> sv
199 val hv_iternextsv : hv -> string * sv
200
201 val get_sv : ?create:bool -> string -> sv
202   (** Return a scalar value by name. For example, if you have a symbol
203     * called [$a] in Perl, then [get_sv "a"] will return its value.
204     *
205     * If the symbol does not exist, this throws [Not_found].
206     *
207     * If the optional [?create] argument is set to true and the symbol does
208     * not exist, then Perl will create the symbol (with value [undef]) and
209     * this function will return the [SV] for [undef].
210   *)
211 val get_av : ?create:bool -> string -> av
212 (** Same as {!Perl.get_sv} except will return and/or create [\@a]. *)
213 val get_hv : ?create:bool -> string -> hv
214 (** Same as {!Perl.get_sv} except will return and/or create [%a]. *)
215
216 val call : ?sv:sv -> ?fn:string -> sv list -> sv
217 (** Call a Perl function in a scalar context, either by name (using the [?fn]
218   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
219   *
220   * Returns the Perl [SV] containing the result value. (See
221   * {!Perl.int_of_sv} etc.).
222   *
223   * If the Perl code calls [die] then this will throw [Perl_failure].
224   *)
225
226 val call_array : ?sv:sv -> ?fn:string -> sv list -> sv list
227 (** Call a Perl function in an array context, either by name (using the [?fn]
228   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
229   *
230   * Returns the list of results.
231   *
232   * If the Perl code calls [die] then this will throw [Perl_failure].
233   *)
234
235 val call_void : ?sv:sv -> ?fn:string -> sv list -> unit
236 (** Call a Perl function in a void context, either by name (using the [?fn]
237   * parameter) or by calling a string/CODEREF (using the [?sv] parameter).
238   *
239   * Any results are discarded.
240   *
241   * If the Perl code calls [die] then this will throw [Perl_failure].
242   *)
243
244 val eval : string -> sv
245 (** This is exactly like the Perl [eval] command. It evaluates a piece of
246   * Perl code (in scalar context) and returns the result (a Perl [SV]).
247   *)
248
249 val call_method : sv -> string -> sv list -> sv
250 (** [call_method obj name [parameters]] calls the method [name] on the Perl
251   * object [obj] with the given parameters, in a scalar context. Thus this
252   * is equivalent to [$obj->name (parameters)].
253   *
254   * Returns the Perl [SV] containing the result value.
255   *
256   * If the method calls [die] then this will throw [Perl_failure].
257   *)
258
259 val call_method_array : sv -> string -> sv list -> sv list
260 (** Like [call_method], but the method is called in an array context. *)
261
262 val call_method_void : sv -> string -> sv list -> unit
263 (** Like [call_method], but the method is called in a void context (results
264   * are discarded). *)
265
266 val call_class_method : string -> string -> sv list -> sv
267 (** [call_class_method classname name [parameters]] calls the static method
268   * [name] in the Perl class [classname] with the given parameters, in a
269   * scalar context. Thus this is equivalent to [$classname->name (parameters)].
270   *
271   * Returns the Perl [SV] containing the result value.
272   *
273   * If the static method calls [die] then this will throw [Perl_failure].
274   *)
275
276 val call_class_method_array : string -> string -> sv list -> sv list
277 (** Like [call_class_method], but the method is called in an array context. *)
278
279 val call_class_method_void : string -> string -> sv list -> unit
280 (** Like [call_class_method], but the method is called in a void context. *)