mod_caml Comes Of Age
Richard W.M. Jones writes "mod_caml is a set of bindings between Objective Caml and the full Apache API. mod_caml 0.6 has bindings for the Apache API and a full Perl-like CGI and templating library. There's only two things you need to know about Objective Caml: it's a modern, fully-featured and highly-optimised language, and it has a good tutorial so Perl/Java/C/C++ programmers can join in the fun."
I've noticed that the home page is kind of slow. There's the original, old one at INRIA.
HTH
Since CAML is a functional language, it's probably more productive to compare it with other functional languages than with more familiar procedural languages. Good stuff here and here. In this context, it makes sense to have a particular look at Guile, which is like mod_caml in that it implements a functional language as a means of writing application extensions.
Actually, the procedural elements of OCaml are so strong than, other than syntax (which is more convenient for functional composition than for imperative sequencing), it is an absolute joy to program UN*X-type filters and transformers in it.
OCaml has a very complete interface to UN*X (including sockets), supports native (POSIX) threading, lots of publicly available libraries exist from regexps to XML parsing, etcetera; so it can be (and is!) used for systems-level programming, much as you would use C or Java.
The only thing that may put down programmers accustomed to the more forgiving, dynamic nature of scripting languages like Perl is that OCaml's type system is very strict, much more so than Java's. On the other hand, it has type inference, whereas you rarely if ever need to type-declare things (other than for documenting, that is).
For the sake of example, this is how you would program wc in OCaml:
Sorry for the ugly formatting, but Slash is unforgiving
Yes, mod parent down; but mod gradparent up.
Caml is the best language ever: Take a look
at the annual programming language results
for Ocaml:
ICFP'02 (1st prize)
ICFP'01 (3rd prize)
ICFP'00 (1st prize)
ICFP'00 (2nd prize)
ICFP'99 (1st prize)
ICFP'98 (2nd prize)
C++ and Java never won anything, even
though there are always many more C++ and
Java contestants than OCaml ones.
Christ, that's "good for scripting"? Here's a Ruby version:
I'd call a language like that "good for scripting". Doesn't get in your way.
Same in OCaml :
:= !r + i
open Str
let (+=) r i = r
let lines = ref 0 and words = ref 0 and chars = ref 0
let _ = try while true do
let s = read_line () in
chars += String.length s;
lines += 1;
words += List.length (split (regexp "\\( \\)+") s)
done
with _ -> Printf.printf "%d %d %d\n" !lines !words !chars
http://www.merjis.com/developers/mod_caml/docs.sht ml
Where possible I've gone for reducing the amount of code that you have to write in the common cases, based on a large amount of experience writing CGI scripts in Perl.
At the moment we're missing a fully integrated database layer, but that's coming soon (the code is already out there, I just need to pull it in and do the persistent database connection stuff).
Unfortunately I only get to work on this at weekends, but hope to have a Savannah page up soon so others can more easily contribute.
Rich.
[1] It's not really "scripting" as such. OCaml programs are bytecode compiled and dynamically linked into the bytecode interpreter which runs inside Apache. We also hope to get natively compiled loading working at some point.
libguestfs - tools for accessing and modifying virtual machine disk images
Yes. There's two ways to do this - either just create the value at the top level of the script. Top level functions in a script get called once when the script is first loaded. The run function (which you have to register) gets called on each invocation.
Thus:
(* foo is evaluated just once: *)let foo = expensive_function ()
let run r = ... (* generate the page each time - see examples in manual*)
let () = Registry.register_script run
How far along is it? Does it have any kind of session support or is that up to the application programmer?
It's still in beta, but please play with it! Without people playing with it it will be perpetually beta software.
Rich.
PS. You might want to continue this conversation by email because Slashcode sucks for posting code snippets ....
libguestfs - tools for accessing and modifying virtual machine disk images
One can really do better in OCaml for scripting purposes than in the initial version:
:= !words + List.length (Pcre.split line); := !chars + String.length line + 1);
:)
let lines, words, chars = ref 0, ref 0, ref 0 in
Pcre.foreach_line (fun line ->
incr lines;
words
chars
Printf.printf "%d %d %d\n" !lines !words !chars
Pcre for OCaml is a very high-level and efficient string processing library that can do just about anything that Perl offers for string manipulation - only much faster
Only in a very limited sense could Python be considered strong typing. It's stronger than C, but most anything is
There are a variety of languages that allow you to cast raw memory addresses to whatever you like: C, C++, Forth, Assembly language, many basics, etc. Those are weakly typed languages as the term was originally applied. Python is a strongly, dynamically typed language. And object has exactly one type and you cannot convince it to behave as another type merely by asserting it is that other type. When you try to get a Python object to do something disallowed by its type, you get an exception whereas in the languages described earlier the results are undefined.