Solved

how do i read data from a file?

Posted on 1998-11-02
2
187 Views
Last Modified: 2010-03-04
I am using perl 5.003, IIS3 and NT4.0

I am desperately trying to get my web server to open a file and read data from it and print out what is read in using a perl script.

Here is what's happening;

This does not give me an error:
 
   print "line1";
   open (FH, "data.txt");
   $line2=<FH>;
   close (FH);

It simply prints 'line1' (as expected)

This does give me an error:

   print "line1";
   open (FH, "data.txt");
   $line2=<FH>;
   print "$line2";
   close (FH);

I get an error that says the "document contains no data"
It doesn't choke on opening, reading, or closing, but it seems to choke when i try to print out what i've read.
It won't even print out line1 when this happens.

What am i doing wrong?
0
Comment
Question by:johnsm
2 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1205892
Ignoring error codes, and forgetting that STDERR and STDOUT are not the same, nor are they buffered similarly? Try doing

$| = 1; # Get rid of this when you're done debugging.
print "Content type: text/html\n";
print "line 1\n\n";
open(FH, "data.txt") || die "Open failed: $!"
$line2 = <FH>;
close(FH);
print "$line2";


0
 
LVL 1

Author Comment

by:johnsm
ID: 1205893
thanks,
that worked fine.


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 102
perl script to search for files containing text 3 89
Perl string filter 5 84
Create an automated page index 9 76
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question