Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: How to convert a text file to a HTML file

by choroba (Bishop)
on Jan 25, 2013 at 12:03 UTC ( #1015332=note: print w/replies, xml ) Need Help??

in reply to How to convert a text file to a HTML file

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 $!;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: How to convert a text file to a HTML file
by Utilitarian (Vicar) on Jan 25, 2013 at 12:20 UTC
    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."


      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 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

        html generally ignores white space, so if you want it displayed in a particular layout then you need to add some markup.

        Try wrapping your text in a pre tag i.e.  <pre>your text here</pre>

        BTW the browser knows how to display different file types, but if your app tells the browser it's sending html and then just sends plain text, it's not surprising that you don't get the results you expected.

        You've posted no code, why make people guess what $htmldata is? You don't show us how you read this input, what processing is done on the input or how you display your output. If all you do is read the data into a variable and print it within a webpage it isn't going to display in the same manner it did within your text editor. You need to learn about HTML, and basic perl. Rather than have to reiterate your problem consider putting some thought into your posts, and asking a real question, How do I post a question effectively?.


        Yes it is just a single line , so I did not put a code. But I have mentioned that I am fetching the concatenated string from the database and I assumed that people would understand $htmldata is holding that value fetched from database.

        I am using a fetchrow_hashref function to fetch the data .


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015332]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-06-22 11:57 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.