Show contents of text file...

Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

Ok, I think this is pretty easy. What I need to do is show the contents of a text file (multiple lines) in a CGI generated HTML file. How do I do this?

------------------
The fireburn.com Network:

  • fireburn.com [www.fireburn.com]
  • Flame Hosting [www.flamehosting.com]
  • Ineffable Designs [www.ineffabledesigns.com]
  • The Webmasters Portal [www.webmasters-portal.com]
  • They have: 850 posts

    Joined: Jul 1999

    Try somthing like:

    open(IN,"<file.txt");
    @data = <IN>;
    close(IN);

    print "Content-type:text/html\n\n";
    print @data;

    This would open file.txt, put each line as an element in an @array and than print the @array.

    Is that what you needed or no?

    ------------------
    Windmills always turn counter-clockwise. Except for the windmills in Ireland.

    They have: 568 posts

    Joined: Nov 1999

    let me add on to that
    if you want to replace every \n (standerd UNIX line break) with a <br> tag use a regular expression

    open(file,"file.txt");
    local($/);
    $file = <file>;
    close(file);

    $file =~ s/\n/<br>/;
    print $file;

    hope that helps

    Justin S's picture

    They have: 2,076 posts

    Joined: Jun 1999

    Ok, I think that's what I want, but it's giving me a 500 Internal Server Error. Here is the code:

    code:

    #!/usr/bin/perl
    
    ########################################################
    ## Counter                                            ##
    ## www.fireburn.com                                    ##
    ########################################################
    
    ########################################################
    ## Variables                                          ##
    ########################################################
    
    ## Path to the counter file
    $countfile = "/home/fireburn/public_html/cgi-bin/count/count.dat";
    
    ## Path to the log file
    $logfile = "/home/fireburn/public_html/cgi-bin/count/log.dat";
    
    ########################################################
    ## Code                                               ##
    ########################################################
    
    open(log,"<$logfile");
    @data = <log>;
    close(log);
    
    print "Content-type:text/html\n\n";
    print @data;[/code]
    
    All the other files work right and are doing there job except this one... I know I chmoded it right also.
    
    ------------------
    The fireburn.com Network:
    
  • fireburn.com [www.fireburn.com]
  • Flame Hosting [www.flamehosting.com]
  • Ineffable Designs [www.ineffabledesigns.com]
  • The Webmasters Portal [www.webmasters-portal.com]
  • They have: 161 posts

    Joined: Dec 1999

    Heh, I see the problem. From the perldata documentation:

    Because variable and array references always start with '$', '@', or '%', the "reserved" words aren't in fact reserved with respect to variable names. (They ARE reserved with respect to labels and filehandles, however, which don't have an initial special character. You can't have a filehandle named "log", for instance. Hint: you could say open(LOG,'logfile') rather than open(log,'logfile'). Using uppercase filehandles also improves readability and protects you from conflict with future reserved words.)

    The section in bold is the most relevant. It is considered good Perl style to use words in all capital letters for filehandles. Your problem is the fact that "log" is the name of a function in Perl. Try using "LOG".

    ------------------
    --
    MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve

    Justin S's picture

    They have: 2,076 posts

    Joined: Jun 1999

    Aha! Thanks a lot! I got it to work now...

    ------------------
    The fireburn.com Network:

  • fireburn.com [www.fireburn.com]
  • Flame Hosting [www.flamehosting.com]
  • Ineffable Designs [www.ineffabledesigns.com]
  • The Webmasters Portal [www.webmasters-portal.com]
  • Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.