Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Printing data structures as HTML

by sutch (Curate)
on Aug 14, 2004 at 03:29 UTC ( #382887=perlquestion: print w/ replies, xml ) Need Help??
sutch has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for some code or a module to print arbitrary perl data structures as HTML, for display in a web browser.

So far I've found:

  • Data::HTMLDumper - Dumps a data structure to an HTML table. Does not escape special HTML characters such as &, < and >. Does not allow for changing the table's attributes.
  • Data::TreeDumper::Renderer::DHTML - Renders a data structure as DHTML (JavaScript is required).
  • Comment on Printing data structures as HTML
    Re: Printing data structures as HTML
    by ikegami (Pope) on Aug 14, 2004 at 09:57 UTC
      Why don't you copy the contents of Data::HTMLDumper into a sub of your own and customize it? (license permitting)
        I had begun to do this, after an unsuccessful attempt to contact the author of the module.

        Data::HTMLDumper pretty much takes the output of Data::Dumper and uses regular expressions to replace characters (such as {, }, ], and =>) with HTML table elements. To me, this seems like a roundabout way of handling this.

        If I'm going to create a module to generate HTML from a data structure, I'd prefer to have one conversion only: from the actual data structure to the final HTML.

    Re: Printing data structures as HTML
    by Fletch (Chancellor) on Aug 14, 2004 at 13:38 UTC

      YAML inside of a <pre /> block is pretty readable. At any rate it might be easier to parse than Data::Dumper output.

    Re: Printing data structures as HTML
    by stvn (Monsignor) on Aug 14, 2004 at 15:05 UTC

      What kind of arbitrary perl data structures are you trying to render (they are not all created equal)? Are you looking for something quick and dirty for debugging purposes? Or more stable for UI purposes? Is there an issue with using Javascript (you seem to imply that with the Data::TreeDumper::Renderer::DHTML comment)? Do you really want it in HTML, or do you just need to see it in the browser? I ask these questions because there are a number of ways you could do this.

      If you have an array of hashes, a few scalars and want a nice UI, you could very easily use HTML::Template.

      If you need to see your structure for debugging purposes, then the output of Data::Dumper wrapped in a PRE tag is usually sufficient (IMO of course).

      If this is for a UI, is it really an arbitrary data-structure? I would expect that there is some consistency and logic to it if it is meant to be displayed in a UI, otherwise you might want to rethink your UI (IMO consitency is a major factor in the usability of a UI).

      The more details you provide, the better we can answer your question.


    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://382887]
    Approved by roju
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2014-08-23 20:58 GMT
    Find Nodes?
      Voting Booth?

      The best computer themed movie is:

      Results (178 votes), past polls