Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to convert a text file to a HTML file

by ghosh123 (Monk)
on Jan 25, 2013 at 11:34 UTC ( #1015328=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk ,

Suppose I have a text file which may contain some data in simple paragraph format or in tabular format.

How can I convert such a file to a html file in Perl ?
Thanks.

Comment on How to convert a text file to a HTML file
Re: How to convert a text file to a HTML file
by põhjapõder (Novice) on Jan 25, 2013 at 12:02 UTC
    Parse the input file and generate HTML based on the resulting parse tree.
Re: How to convert a text file to a HTML file
by choroba (Abbot) on Jan 25, 2013 at 12:03 UTC
    Just add the header and footer:
    #!/usr/bin/perl use strict; use warnings; open my $HTML, '>', 'output.html' or die $!; print $HTML <<'_END_HEADER_'; <html> <head><title></title></head> <body> _END_HEADER_ open my $IN, '<', 'input.txt' or die $!; while (my $line = <$IN>) { # You probably want to do more work here. print $HTML $line; } print $HTML '</body></html>'; close $HTML or die $!;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Meh, why do any work ;)
      #!/usr/bin/perl use strict; use warnings; open my $HTML, '>', 'output.html' or die $!; print $HTML <<'_END_HEADER_'; <html> <head><title></title></head> <body> <pre> _END_HEADER_ open my $IN, '<', 'input.txt' or die $!; print $HTML while <$IN>; print $HTML '</pre></body></html>'; close $HTML or die $!;
      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

        Hi

        Thanks for your input. Now let me re-iterate my problem this way. Suppose I have simple a.txt(pasted below) file in my desktop. When I open that file on the browser by giving the unix path/windows path ...it comes in proper order and indentation.

        But when I am reading this file in a string and inserting the string in a database column and later on using cgi perl printing it on the webpage , it gets messed up.

        What magic is happening when I do print $htmldata in my cgi perl code so that it destroys the order and indentation of the text ?

        name    phone

        xyz    123

        abc    890

        But it is coming as :

        name phone xyz 123 abc 890 Thanks

Re: How to convert a text file to a HTML file
by Corion (Pope) on Jan 25, 2013 at 12:04 UTC

    Maybe one of the Formatter modules can help, or maybe your input text looks good when rendered through Markdown.

Re: How to convert a text file to a HTML file
by flexvault (Parson) on Jan 25, 2013 at 14:14 UTC

    ghosh123,

    Did you try Utilitarian 's code?

    "Well done is better than well said." - Benjamin Franklin

Re: How to convert a text file to a HTML file
by Anonymous Monk on Jan 26, 2013 at 02:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015328]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-09-30 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (381 votes), past polls