rsriram has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am sorry if I am hosting a basic question! But I am new to CGI Perl.

I want to open a XML file and display its contents in the web. The script I wrote is

#!C:\perl\bin\perl.exe -wT use strict; use CGI qw/:standard/; print header, open (FILE, "c:/perl/directory.xml"); while (<FILE>){ chomp; print "$_\n"; }

But in the internet explorer, I see all the lines in the file running on continiously, i.e., \n doesn't seem to work. Can anyone help me out?

Replies are listed 'Best First'.
Re: Displaying carriage returns
by imp (Priest) on Aug 11, 2006 at 13:14 UTC
    You need to tell the browser what sort of content it should be displaying.

    To display as plaintext:

    print header('text/plain');
    To use whatever method the browser defaults to for xml:
    print header('text/xml');
    Personally I find the CGI header options limiting and tend to print them manually, e.g.:
    print "Content-type: text/xml\n"; print "Content-Disposition: inline; filename=directory.xml\n\n";
    Note that the initial output is treated as headers that define what is to come, which is terminated by two '\n'.

    The reason for including the Content-Disposition header in this case is to provide an extra hint to browsers that care about file extensions, like IE.

Re: Displaying carriage returns
by davorg (Chancellor) on Aug 11, 2006 at 13:42 UTC

    You've already got good answers to your question, but I wanted to point out a small inefficiency in your program.

    while (<FILE>){ chomp; print "$_\n"; }

    What is the point of removing the carriage return on one line, only to add another one to the end of the record on the very next line?

    That's better written as:

    while (<FILE>) { print; }

    Or even just:

    print while <FILE>;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Displaying carriage returns
by liverpole (Monsignor) on Aug 11, 2006 at 13:13 UTC
    Hi rsriram,

    As monkey_boy said, this really is an HTML question.  And his suggestion of putting <pre> ... </pre> tags around it is a very good idea.

    You could also use <p> tags before every new paragraph.  Or <br>, to cause a line break.

    By the way, you really should check the result of opening files; eg.:

    open (FILE, "c:/perl/directory.xml") or die "Can't read file 'director +y.xml' ($!)\n";

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Displaying carriage returns
by marto (Cardinal) on Aug 11, 2006 at 13:14 UTC
    Hi rsriram,

    I am not sure if this answer is correct, I can't test this right now as I am at work. If you change the header to be XML (text/xml) rather than HTML (which is the default according to the CGI documentation).

    Hope this helps.

    Martin
Re: Displaying carriage returns
by monkey_boy (Priest) on Aug 11, 2006 at 13:01 UTC
    This is a HTML question really, but why not just stick the whole thing in "pre" tags?.



    This is not a Signature...
      Hi monkey boy,

      I think rsriram wants to retain the functionality IE has when displaying XML documents, the ability to show/hide (+ and -) sections of the document etc.

      Martin
Re: Displaying carriage returns
by duckyd (Hermit) on Aug 11, 2006 at 18:25 UTC
    This question really makes me wonder why you don't just place the xml file in your document root, and let your webserver (IIS? apache?) serve it directly...
Re: Displaying carriage returns
by bart (Canon) on Aug 12, 2006 at 09:13 UTC
    Perhaps you could use CSS. There's an attribute white-space which allows you to make tags behave like <pre> tags for whitespace, without altering the font or anything else.

    I have no idea how compatible this is with preserving the default outline showing behaviour in browsers.

Re: Displaying carriage returns
by msalerno (Beadle) on Aug 11, 2006 at 17:54 UTC
    Initially I wrote that you could use a s/\n/<br>/gsx; To convert the new lines to html line breaks, but then I realized that you want to view xml, and it would have broken your xml.