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

code troubleshooting

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I needed a perl thing that read the number of lines in a text file and printed them on the screen. I asked on a board and was given this code:

open(FILE, &quot;<file.txt&quot;) or die 'couldn't open file.txt';
@lines = <FILE>;
close(FILE);
print &quot;The number of lines in file.txt is: $#lines&quot;;

That, with the shebang line #!/usr/bin/perl in the beginning, didn't work (gave a 500 error). I know the shebang is correct, the permissions were set correctly, file.txt existed, so what am I doing wrong? Is there something wrong with the code I was given, or do I need to add something more to the code for it to be a complete perl program?
 
I don't remember just what a 500 error is. Can you give the text of the error message?

If you're printing to a browser, you have to print an http content-type header first. Unless you want to print html, try this:
Code:
print &quot;Content-type: text/plain\n\n&quot;;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
That error is usually caused by a syntax error.

Is this line exactly what you have in your program?
Code:
open(FILE, &quot;<file.txt&quot;) or die 'couldn't open file.txt';
If so, take a look at it again. Here's a hint, colored to highlight the problem:
Code:
open(FILE, &quot;<file.txt&quot;) or die
'
Code:
couldn
'
Code:
t open file.txt
'
Code:
;
See the problem? You've got an apostrophe nested between apostrophes. Try changing the outer apostrophes to quotes. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Are you running this as a standalone program, or as a cgi program?

What directory does file.txt exist in?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Can you tell us what is the error message ?

===================
::) Juan F. Sarria
 
The error message should appear in the error_log for your server process. Alternatively, try adding the following line to the top of your code, which should output errors to the browser window:
Code:
use CGI::Carp qw( fatalsToBrowser );
Cheers, Neil
 
the $#lines give you the index of the last element in the array, just a little note... you could use scalar(@lines) though.

//Daniel
 
Just as a little question (I'm not gonna bother starting a new thread), is there any way to get CGI::Carp to give more useful error messages? When I do not include the line and examine the log file, it gives the error message and the line number. But CGI::Carp just reports the extremely useless &quot;execution of file.cgi aborted due to compilation errors&quot; most of the time. Is there any way to get it to report what is in the regular log file?
 
Grahf
Looks like compilation errors are a little tricky. Perhaps one way would be to have a wrapper CGI script that calls other scripts in an eval block, and handles compilation errors that way.
Neil
 
Ok I've found the problem. Think I uploaded as binary and not ascii... Thats just too dumb :)
 
That can't be dumb bananajam - because I've done it as well Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
dont' forget:

- before you upload anything, do >perl -c code.cgi to check you syntax

- upload as ascii ( you obviously remembered this one)

- convert to unix style line endings if you are uploading from a PC to a *nix box

- set the execute bits, if uploading to a *nix box.

I have made all of these mistakes repeatedly over the years.

I once read a definition of insanity - doing the same thing over and over again, each time expecting a different result and each time getting the same result. Seems like maybe I'm a little insane. I keep making these same errors. At least they are very easy to fix. (The best kind of problem, in my opinion.)

Happy Coding! If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
I don't mean to be picky, but isn't making sure you upload as ascii and converting to unix-style line endings part of the same thing? The ONLY thing that an ascii upload does that a binary upload doesn't do is convert line endings (CRLF to LF). Or am I forgetting something?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
You are correct. I am being redundant and consequently misleading (sorry). I have resorted to listing those separately because I have found a number of instances where someone thinks they have done everything they need and actually have not. When I start trouble shooting most CGI problems for others, they are very frequently fixed with dos2unix, even when the struggling newbie swears they already did that. Sorry for the misdirection. If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Well it certainly doesn't hurt to emphasize that step by describing it in two different ways, since, as you point out, it's an easy mistake to make and a very difficult one to catch. I've driven myself crazy with that problem once or twice. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top