Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Printing contents of file on a HTML page.

by Anonymous Monk
on Sep 23, 2002 at 15:41 UTC ( #200139=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If I have a file on my Windows NT called "bas.txt" with contents of:
wordOne
wordTwo
wordThree

Then I would just want to print this on a web page like this:
wordOne
wordTwo
wordThree


I can make a page that prints out something but how would I get the contents of a file and print it out on a web page? Here is my attempt and the output:
use strict; my $path = "/web/webpage.html"; my $myfile = "/perl/bin/bas.txt"; open FIL, "$myfile" || die "cant open: $!\n"; open OUT, ">$path" || die "Can't open $path for writing: $!\n"; print OUT <<"EOF"; <html> <head> <title>Title</title> </head> <body> Okay this prints out.<br> $myfile </body> </html> EOF close OUT || die "Can't close filehandle: $!\n"; close FIL || die "no cant close: $!\n";
The output from this:
Okay this prints out. /perl/bin/bas.txt
I need the output to be the contents of the file not the path name, so I am doing something wrong. The output should be:
Okay this prints out. wordOne wordTwo wordThree
Please advise what I need to change or do to make this work???

Comment on Printing contents of file on a HTML page.
Select or Download Code
Re: Printing contents of file on a HTML page.
by dreadpiratepeter (Priest) on Sep 23, 2002 at 15:51 UTC
    You are printing the name of the file not the contents. You need to use the <> operator to read through the file and print out the results. ie:
    foreach (my $line = <FIL>) { print OUT $line; }

    You can read about FileIO in O'Reilly's Learning Perl. I highly recommend it.

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
      Or, if you like brevity:
      while (<FIL>) { print; }
      (print, like many Perl functions defaults to using the variable $_ if no other arguments are given. The loop stores each line of the file in $_ if it is not asigned to any other variable)
        Sorry - I forgot to log in. That should have read:
        while (<FIL>) { print OUT; }
        I know. He seemed like a beginner so I wanted to be clear. You could also do:
        print while <FIL>; print join('',<FIL>);
        etc.

        -pete
        "Pain heals. Chicks dig scars. Glory lasts forever."
Re: Printing contents of file on a HTML page.
by sch (Pilgrim) on Sep 23, 2002 at 15:56 UTC

    Basically you haven't specified that the contents of the file is to be printer anywhere, only the name of the file - in this section of code

    <body> Okay this prints out.<br> $myfile </body> </html>
    You're not specifying that the contents of file $myfile be printed, but rather the value of variable $myfile which you've set to "/perl/bin/bas.txt";

    What you need is something that will print out the HTML header info - I'd suggest using the CGI module rather than hand coding, and then a loop to read through the file printing out the contents.

•Re: Printing contents of file on a HTML page.
by merlyn (Sage) on Sep 23, 2002 at 16:15 UTC
    In addition to the other comments in this thread...

    If the file is the entire contents of your response, you should make sure to make the response have a text/plain MIME type, or the browser will interpret entities and tags incorrectly.

    If the file is part of a larger response, you'll need to encode the entities. The simplest way to do that is with the HTML::Entities module:

    use HTML::Entities qw(encode_entities); print "<pre>"; print encode_entities($_) while <HANDLE>; print "</pre>";

    -- Randal L. Schwartz, Perl hacker

Re: Printing contents of file on a HTML page.
by DamnDirtyApe (Curate) on Sep 24, 2002 at 01:28 UTC

    You might be interested in using Template Toolkit for this. First of all, you can forego the process of reading the file, and simply insert it into the template. Consider the following code:

    #! /usr/bin/perl use strict ; use warnings ; $|++ ; use Template ; my $tmpl = <<'END_OF_TEMPLATE' ; Content-type: text/html <html> <head> <title>Title</title> </head> <body> <p>Okay this prints out.</p> [% INSERT $myfile %] </body> </html> END_OF_TEMPLATE my $tt = new Template { 'ABSOLUTE' => 1 } ; my $filename = '/home/doug/.bashrc' ; $tt->process( \$tmpl, { 'myfile' => $filename } ) or die $tt->error() +; exit ;

    This should do what you want, while at the same time giving you some added flexibility on how you deal with the file contents. For example, if you want to escape all the HTML tags in the included file, you'd say [% INSERT $myfile | html %]. If you wanted to take a text file that was broken up into paragraphs and retain that formatting, you'd say [% INSERT $myfile | html_para %]. If you wanted to preserve the line breaks in the input file without using <pre>..</pre> tags, try [% INSERT $myfile | format( "%s<br />" ) %]. Read the docs for more info.

    HTH


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2014-09-21 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (168 votes), past polls