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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with my code: tryign to read a text file

Status
Not open for further replies.

romerz

IS-IT--Management
Jul 19, 2006
29
GB
#!/usr/bin/perl

print "Content-type: text/plain\n\n";

open(FH,"test.txt") or &dienice("Can"t open test.txt: $!");
my @ary = <FH>;
close(FH);

foreach my $line (@ary) {
print $line;
}



 
Reading the error message might help. [wink]

And if you are unable to understand the error message yourself, you should at least include it in your post, in order to help others to help you.

A hint:
You cannot simply include a " between two "".
replace it by ', or by \
 
oh yeah haha -

no error messages come up. The screen goes blank :(
 
Ok, I tried your code and got an error message;
try the change I suggested in my previous post!
 
the code is now.

#!/usr/bin/perl

print "Content-type: text/plain\n\n";

open(FH,"test.txt") or &dienice("Cant open test.txt: $!");
my @ary = <FH>;
close(FH);

foreach my $line (@ary) {
print $line;
}

blank pages apears :/
 
on the command line type the following (assuming windows)
Code:
perl -c script.pl

As well as the error hoinz pointed out, I don't see any reference to the dienice procedure that's being called in the event of error

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
For me it worked, on Unix, when I replaced can"t by can't ...
 
#!/usr/bin/perl

print "Content-type: text/plain\n\n";

open(FH,"test.txt") or die("Cant open test.txt: $!");
my @ary = <FH>;
close(FH);

foreach my $line (@ary) {
print $line;
}

is what its at and still get no error messages.

perl is running on my web hosting not my pc - so I have no access to the command line.

All i want to do is show the contents of a txt file - wether than be in the browser or if the txt file just opens.

If you have a better way or "working" way hah that would be brilliant.
 
Are you sure that test.txt has some contents to be printed out?

Also, if you insist on cross-posting your questions, it's considered common courtesy to at least mention that other people are helping with the problem elsewhere.
 
or to be more precise:
it worked, when there was a file test.txt
Otherwise I get:
Undefined subroutine &main::dienice called at ./tt.pl line 5.

PaulTEG has spotted that.
 
There's your problem so, as I've mentioned in your other thread. You don't have any subroutine called "dienice". Usually when people do that, they define something like this (putting in HTML code when it's a HTML page they're generating):
Code:
sub dienice {
   print @_;
}
 
Yeah test.txt has 5 lines, each with the words TEST1 - TEST5 on them :x
 
That's strang now.
The code posted on 19 Jul 06 6:34 works for me, no matter whether the file is there ...
 
lol now im really confused.

Just to make 100% sure

/cgi-bin/cjc/in_out/newsletter_track/newsletter_track.html

form action="
which posts to call_newsletter.pl which is in
/cgi-bin/cjc/in_out/newsletter_track/call_newsletter.pl


#!/usr/bin/perl

print "Content-type: text/plain\n\n";

open(FH,"test.txt") or die("Cant open test.txt: $!");
my @ary = <FH>;
close(FH);

foreach my $line (@ary) {
print $line;
}


and then there is


/cgi-bin/cjc/in_out/newsletter_track/test.txt

which has 5 lines
TEST1
TEST2
TEST3
TEST4
TEST5
 
Is there a question there?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
maybe your real problem, although this probably sounds stupid, is all the shorthand you're using. try expanding it all and you might find your problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top