Slashdot Mirror


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."

10 of 43 comments (clear)

  1. Old caml page by notfancy · · Score: 2, Informative

    I've noticed that the home page is kind of slow. There's the original, old one at INRIA.

    HTH

  2. Improved linkage by fm6 · · Score: 4, Informative
    The link to Bagley.org gets diverted because Doug Bagley is a Slashdot-hater. You can read his summary of CAML by cutting and pasting
    http://www.bagley.org/~doug/shootout/
    into your address bar.

    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.

  3. Re:O'Caml for Scripting? by notfancy · · Score: 5, Informative

    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:

    type counts = {
    mutable lines: int;
    mutable words: int;
    mutable chars: int;
    }

    let wc fname =
    let inch = open_in fname
    and stat = { lines = 0; words = 0; chars = 0 }
    in begin
    try while true do
    begin
    match input_char inch with
    '\n' -> stat.lines <- stat.lines + 1;
    stat.words <- stat.words + 1
    | '\t'
    | ' ' -> stat.words <- stat.words + 1
    | _ -> ()
    end;
    stat.chars <- stat.chars + 1
    done with End_of_file -> ()
    end;
    close_in inch;
    stat

    Sorry for the ugly formatting, but Slash is unforgiving

  4. Re:Mod gradparent up! by Anonymous Coward · · Score: 1, Informative

    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.

  5. Re:O'Caml for Scripting? by Anonymous Coward · · Score: 1, Informative

    Christ, that's "good for scripting"? Here's a Ruby version:

    lines = words = chars = 0

    while gets
    lines += 1
    words += $_.scan(/\S+/).length
    chars += $_.length
    end

    puts "#{lines} #{words} #{chars}"

    I'd call a language like that "good for scripting". Doesn't get in your way.

  6. Re:O'Caml for Scripting? by Anonymous Coward · · Score: 1, Informative

    Same in OCaml :

    open Str

    let (+=) r i = r := !r + i

    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

  7. Re:O'Caml for Scripting? by Richard+W.M.+Jones · · Score: 4, Informative
    I've tried to make mod_caml "scripting"[1] as simple as possible. There's a few example scripts in the manual here:

    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.

  8. Re:O'Caml for Scripting? by Richard+W.M.+Jones · · Score: 2, Informative
    Persistent cached data? So ocaml values can be instantiated once and then shared for the whole application like with a Java appserver?

    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 ....

  9. Re:O'Caml for Scripting? by Anonymous Coward · · Score: 2, Informative

    One can really do better in OCaml for scripting purposes than in the initial version:

    let lines, words, chars = ref 0, ref 0, ref 0 in
    Pcre.foreach_line (fun line ->
    incr lines;
    words := !words + List.length (Pcre.split line);
    chars := !chars + String.length line + 1);
    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 :)

  10. Re:O'Caml for Scripting? by smallpaul · · Score: 4, Informative

    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.