Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file

Status
Not open for further replies.

tsp120

Programmer
May 21, 2003
52
US
Hello. I'm just starting to learn to use Perl and CGI to read a text file and display the information in a webpage. For starters, all I am trying to do is open up the text file and print each line. The text file is located in the same location as the pl file.

I have this code:

Code:
#!/usr/bin/perl

use CGI;
$q = new CGI;
print $q->header;
print $q->start_html();

open(stats, "032406.txt");

print "hello";

while (my $record = <stats>) {
  print "Line: " . $record;
}

print "HELLO";

close(stats);

print end_html;

It successfully prints out both 'hellos', but does print anything from the text file. What might I be doing wrong?

Thanks!
 
What are the permissions of the "032406.txt" file? It could be the the userid that the webserver is running on doesn't have permissions on the text file.
A good practice when opening data files is to check and see if perl was able to open the file. Try this:
Code:
open(stats, "032406.txt") or die "Unable to open file: $!\n";

That will tell you for sure if perl was able to open the file.

Since you are already using the CGI library, put this line near the top of your script as well:
Code:
use CGI::Carp 'fatalsToBrowser';
That will nicely display any errors on your webpage.
 
Thanks! I added that code and I get the die message.

The permissions for the text file are:
-rwxr-xr-x

I don't know how to change them on this webserver. I'm using a free one right now while I get my feet wet. If those are not the correct permissions, what should it be set to?
 
If all you are doing is reading the file, those permissions should be sufficient. Did the webpage give an error? You mentioned you get the die message - what was that message?
 
I got it working, thanks for your help. The problem turned out to be that the web host requires the full directory and file name. I was just giving the file name.

Again, thanks for taking the time to help me.
 
you should be able to use just the filename if the file resides in the same directory as the script. The problem with your script appears to be that you never direct the print commands to print to the filehandle "stats":

Code:
open(stats ">>myfile.txt") or die "$!";
print [b]stats[/b] "Hello\n";
close(stats);
 
KevinADC, tsp120 wants to read the file, not write to it...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
oops.... I swear I read that as:

but does print anything to the text file.


Thanks Steve, I'll go back to my nicely padded cell now [dazed]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top