http://qs1969.pair.com?node_id=601023


in reply to Re: Print out an XML file to browser
in thread Print out an XML file to browser

Hi been42

That is exactly what I am looking to do. I tried your start_html but it just returns a time stamp to the browser. Am I missing something more fundemental? I'm a bit of a CGI novie, but here is what I have so far:

optput:Mon Feb 19 21:34:25 2007 processremote.cgi: readline() on closed filehandle XMLFILE at /my/upload/path/processremote.cgi line 15.

#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print header; print start_html(-head=>meta({-http_equiv => 'Content-Type', -content => 'text/xml'})); open(XMLFILE, "/my/upload/path/master.xml"); while (<XMLFILE>){ print $_; }

Replies are listed 'Best First'.
Re^3: Print out an XML file to browser
by virtualsue (Vicar) on Feb 20, 2007 at 05:56 UTC
    Two things:
    1. If you know you want to output xml, then print header('text/xml'); is a lot simpler.
    2. Always check to see if your 'open' succeeded. Your error message means that your script didn't open the file.
    #!/usr/local/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print header('text/xml'); my $file = "/some/file.xml"; open(XMLFILE, $file) or die "Failed to open $file, $!"; while (<XMLFILE>){ print; }
      Thanks Virtualse

      The above code seems to work but I get a bad headder error. It "works" mening in prints by hand, just not to the browser. What might cause that?
      Thanks for the correction. I thought I remembered that it was something simple, but I guess I just skipped over that in the docs when I looked.
Re^3: Print out an XML file to browser
by siva kumar (Pilgrim) on Feb 20, 2007 at 05:41 UTC
    Please make sure whether you have "master.xml" in the specified location.
    Use the below code.
    open(XMLFILE, "/my/upload/path/master.xml") or die("Can't open file m +aster.xml : $!");
    also its best practice to close the filehandle after the usage.
    close(XMLFILE)
      It isn't really necessary to bother about closing a read-only filehandle. If you want to be rigorous and check everything, then why aren't you checking whether the print statements succeed? :) If you were writing to the file, then I agree - close the filehandle and check to see if an error occurred.
      how does the <while> look?