Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Print Flat File to HTML

by lakeTrout (Scribe)
on Nov 20, 2007 at 06:34 UTC ( [id://651867]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Folks, I have been in and out of perl for a while and I'm a novice, so please bare with me. I searched for a solution, but still came up empty and hope you find folks can help me out.

I have a form that writes to a tab delimited file...works great. However, I want to print those results to an HTML page -- so you can just view the FF as an HTML page/URL. Here is what I have come up with that writes to a FILE called results.html but results.html is blank in the browser. Can you help me find what I'm missing?

Also note the brute force nature, sorry for that, but as I said I'm a novice that just dabs in perl from time to time.
#!/usr/bin/perl open(OUTFILE, ">results.html"); print OUTFILE "<html><head>"; print OUTFILE "<title>Summit Results</title>"; print OUTFILE "</head>"; print OUTFILE "<body>"; print OUTFILE "<table border='1'>"; print OUTFILE "<tr>"; print OUTFILE "<td>Company</td>"; print OUTFILE "<td>Address</td>"; print OUTFILE "<td>Name</td>"; print OUTFILE "<td>Email</td>"; print OUTFILE "<td>Phone</td>"; print OUTFILE "<td>fjskdfjskla</td>"; print OUTFILE "<td>fjsdklfja</td>"; print OUTFILE "<td>fhjsdhfj</td>"; print OUTFILE "<td>Pfjdskl</td>"; #... there's 25 fileds print OUTFILE "</tr>"; open (FF,"mylogfile.log") or die("There was a problem opening the file +."); while ($entry=<FF>) { @fields=split(/ /,$entry); print OUTFILE "<tr>"; print OUTFILE "<td>$fields[0]</td>"; print OUTFILE "<td>$fields[1]</td>"; print OUTFILE "<td>$fields[2]</td>"; print OUTFILE "<td>$fields[3]</td>"; print OUTFILE "<td>$fields[4]</td>"; print OUTFILE "<td>$fields[5]</td>"; print OUTFILE "<td>$fields[6]</td>"; print OUTFILE "<td>$fields[7]</td>"; print OUTFILE "<td>$fields[8]</td>"; print OUTFILE "<td>$fields[9]</td>"; #... there's 25 fileds print OUTFILE "</tr>"; } close DB; print OUTFILE "</table>"; print OUTFILE "</body>"; print OUTFILE "</html>"; close OUTFILE;
Also, I think I can just do print to >>WHATEVER instead of all of the OUTFILE calls right?

Thanks all!

LakeTrout

Replies are listed 'Best First'.
Re: Print Flat File to HTML
by naikonta (Curate) on Nov 20, 2007 at 06:51 UTC
    The default filehandle is STDOUT so to print to other filehandle you have to explicitly specify it. But you can avoid to express the filehandle if you select it before printing. Always check your open statement.
    open OUTFILE, ">result.html" or die "Can't write to result.html: $!\n" +; select OUTFILE; print "<html><head>"; print "other html stuff";
    You can save the print statement with the here-document style.
    print <<HTML; <html><head> <title>HTML with here-docs</title> ... ... more stuff HTML # do some processing for my $item (@items_from_somewhere) { print "<li>$item\n"; } print <<HTML ... continue HTML ... </body></html> HTML
    But, I think you can get the most from templating system such as HTML::Template. I do.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Print Flat File to HTML
by jZed (Prior) on Nov 20, 2007 at 06:49 UTC
    Or you could accomplish the same thing with this complete script:
    #!/usr/bin/perl use warnings; use strict; use AnyData; adConvert('Tab','somefile.csv','HTMLtable','somefile.html'); __END__
    That takes the tab "delimited" file "somefile.csv" and turns it into an HTML table. It assumes that the columns are stored in the first row of the file, if not, you can easily specify them.
Re: Print Flat File to HTML
by andreas1234567 (Vicar) on Nov 20, 2007 at 06:48 UTC
    results.html is blank in the browser.
    What does results.html look like in a text editor?

    I suggest you start using Log::Log4perl and sprinkle your code with DEBUG statements. Does the split work as you expect?

    use Data::Dumper; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); DEBUG "fields: " . Dumper(\@fields);
    --
    Andreas
      Howdy!

      If I run the script manually, the HTML prints just fine to my terminal. Is this a permission problem perhaps? I've set the permissions, but are they being overwritten somewhere I'm not aware? Pardon the novice questions and I much appreciate your help.

      Thanks!
Re: Print Flat File to HTML
by GertMT (Hermit) on Nov 20, 2007 at 07:10 UTC
    isn't it that: @fields=split(/    /,$entry);
    should be somthing like @fields=split(/\t/,$entry);
      Hi -- yes, that is just sloppy code. It should be /\t/ -- thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://651867]
Approved by andreas1234567
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found