Solved

how do i read data from a file?

Posted on 1998-11-02
2
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

751 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