Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Print out an XML file to browser

by lakeTrout (Scribe)
on Feb 20, 2007 at 04:37 UTC ( #601023=note: print w/ replies, xml ) Need Help??


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 $_; }


Comment on Re^2: Print out an XML file to browser
Download Code
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)
      how does the <while> look?
      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.
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://601023]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2015-07-03 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (55 votes), past polls