Loading ...
Sorry, an error occurred while loading the content.

Data::Dumper equivalent?

Expand Messages
  • Alan Post
    When I do Perl programming, my standard debugging technique is scattering prints of Data::Dumper-ed data structures. This lets me easily visually scan the
    Message 1 of 5 , Oct 17, 2003
    • 0 Attachment
      When I do Perl programming, my standard debugging technique is
      scattering prints of Data::Dumper-ed data structures. This lets me
      easily visually scan the data structures, to make sure they're good.
      Most of my bugs reflect themselves in mangled data structures. It's
      easier to look over a data structure as a Dumper-ed whole than to poke
      through it by hand in a debugger.

      The ocaml toplevel has something similar in the read-eval-print loop.
      Is there any way to invoke the Dumper-ing code from within one's
      ocaml program?
    • Richard Jones
      ... I ve wanted this feature too ... It should be possible to do something using Obj, but you have to remember that because OCaml is strongly typed, the
      Message 2 of 5 , Oct 18, 2003
      • 0 Attachment
        On Fri, Oct 17, 2003 at 08:54:56PM +0000, Alan Post wrote:
        >
        > When I do Perl programming, my standard debugging technique is
        > scattering prints of Data::Dumper-ed data structures. This lets me
        > easily visually scan the data structures, to make sure they're good.
        > Most of my bugs reflect themselves in mangled data structures. It's
        > easier to look over a data structure as a Dumper-ed whole than to poke
        > through it by hand in a debugger.
        >
        > The ocaml toplevel has something similar in the read-eval-print loop.
        > Is there any way to invoke the Dumper-ing code from within one's
        > ocaml program?

        I've wanted this feature too ...

        It should be possible to do something using Obj, but you have to
        remember that because OCaml is strongly typed, the compiler is able to
        (and does) throw away a large amount of type information at runtime.

        For instance:

        ----------------------------------------------------------------------
        open Printf

        (* Make an array containing 3 integers. *)
        let arr = Array.init 3 (fun i -> i)

        (* Make a struct containing 3 integers. *)
        type t = { a : int; b : int; c : int }

        let struc = { a = 0; b = 1; c = 2 }

        (* See what they contain. *)
        let () =
        let arrrepr = Obj.repr arr in
        let strucrepr = Obj.repr struc in
        printf "arr is block? %s\n"
        (if Obj.is_block arrrepr then "true" else "false");
        printf "struc is block? %s\n"
        (if Obj.is_block strucrepr then "true" else "false");
        printf "arr size? %d\n" (Obj.size arrrepr);
        printf "struc size? %d\n" (Obj.size strucrepr);

        let print_fields repr =
        for i = 0 to (Obj.size repr) - 1 do
        (* assume fields are unboxed ints, which is OK for this case *)
        printf "%d " (Obj.obj (Obj.field repr i) : int)
        done
        in

        printf "arr = ["; print_fields arrrepr; printf "]\n";
        printf "struc = ["; print_fields strucrepr; printf "]\n";
        ----------------------------------------------------------------------

        Running this produces:

        ----------------------------------------------------------------------
        arr is block? true
        struc is block? true
        arr size? 3
        struc size? 3
        arr = [0 1 2 ]
        struc = [0 1 2 ]
        ----------------------------------------------------------------------

        So my point is that you can certainly navigate over the structure of
        objects in memory, but a lot of interesting information is missing.

        Rich.

        --
        Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
        Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
        "One serious obstacle to the adoption of good programming languages is
        the notion that everything has to be sacrificed for speed. In computer
        languages as in life, speed kills." -- Mike Vanier
      • Alan Post
        ... Neat. I realize that there is little run-time type information available (the bare minimum needed by the GC, right?) -- but at compile time it should be
        Message 3 of 5 , Oct 18, 2003
        • 0 Attachment
          In article <20031018100222.GA29704@...>, Richard Jones wrote:
          > On Fri, Oct 17, 2003 at 08:54:56PM +0000, Alan Post wrote:
          >>
          >> The ocaml toplevel has something similar in the read-eval-print loop.
          >> Is there any way to invoke the Dumper-ing code from within one's
          >> ocaml program?
          >
          > I've wanted this feature too ...
          >
          > It should be possible to do something using Obj, but you have to
          > remember that because OCaml is strongly typed, the compiler is able to
          > (and does) throw away a large amount of type information at runtime.

          Neat.

          I realize that there is little run-time type information available
          (the bare minimum needed by the GC, right?) -- but at compile time it
          should be possible to generate the relevant dumping code, as indeed
          the toplevel does.
        • Richard Jones
          Some similar code which might be useful: http://www.lri.fr/~filliatr/ftp/ocaml/display/display.ml Rich. -- Richard Jones. http://www.annexia.org/
          Message 4 of 5 , Oct 18, 2003
          • 0 Attachment
            Some similar code which might be useful:

            http://www.lri.fr/~filliatr/ftp/ocaml/display/display.ml

            Rich.

            --
            Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
            Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
            "One serious obstacle to the adoption of good programming languages is
            the notion that everything has to be sacrificed for speed. In computer
            languages as in life, speed kills." -- Mike Vanier
          • Stefano Zacchiroli
            ... Yes, it is actually possible and implemented, as a camlp4 syntax extension, in IoXML. Using IoXML dumper and parser for each type you define are created,
            Message 5 of 5 , Oct 19, 2003
            • 0 Attachment
              On Sat, Oct 18, 2003 at 10:42:03AM +0000, Alan Post wrote:
              > I realize that there is little run-time type information available
              > (the bare minimum needed by the GC, right?) -- but at compile time it
              > should be possible to generate the relevant dumping code, as indeed
              > the toplevel does.

              Yes, it is actually possible and implemented, as a camlp4 syntax
              extension, in IoXML. Using IoXML dumper and parser for each type you
              define are created, as the name suggests XML is used as the input/output
              format.

              IoXML was originally written by Daniel de Rauglaudre and is now part of
              cameleon:

              http://pauillac.inria.fr/~guesdon/Tools/cameleon/cameleon.html

              Cheers.

              --
              Stefano Zacchiroli -- Master in Computer Science @ Uni. Bologna, Italy
              zack@{cs.unibo.it,debian.org,bononia.it} - http://www.bononia.it/zack/
              " I know you believe you understood what you think I said, but I am not
              sure you realize that what you heard is not what I meant! " -- G.Romney
            Your message has been successfully submitted and would be delivered to recipients shortly.