CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Premature end of script headers - involved code

 



S_Shrum
User

Jan 22, 2001, 4:56 PM

Post #1 of 7 (907 views)
Premature end of script headers - involved code Can't Post

I've checked the matching posts but they are all of the state of mind that something is wrong in the script. I don't think that is the case as I am able to run the script perfectly on my NT intranet. I would have posted in the BEGINNER forum but the code is involved (I wrote it from scratch).

Here goes:

I am writting a flatfile to html table script that allows for various input, sorting, display, etc. The script (as I mentioned above) works great under NT (unless of course, NT is more lenient). After I post it on my Unix host, I get errors. I checked the error log and saw:

Premature end of script headers

I posted the latest version of the code at my site. You can take a look at it at:

http://www.shrum.net/db_table.pl

Thanxs to everyone who has helped me in the past (and future =} ).

Sean Shrum
sean@shrum.net



modified
Deleted

Jan 22, 2001, 5:35 PM

Post #2 of 7 (904 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

Your script is missing a shebang line. That's why it doesn't work on Unix, Unix requires a shebang line. Something like this:

#!/usr/bin/perl

As the first line of you code.



modified
Deleted

Jan 22, 2001, 5:39 PM

Post #3 of 7 (904 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

Okay, in Notepad, all the line breaks where messed up, and I opened it up in WordPad. You do have a shebang I can see, but try making it the very first line of the script.



rGeoffrey
User / Moderator

Jan 22, 2001, 9:17 PM

Post #4 of 7 (900 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

Are you sure that your file only has newlines "\n" when it arrives on the UNIX box?

Remember that files in the Windows world use "\r\n" to separate lines, Macs use "\r", and UNIX likes "\n". Sometimes when you FTP files between systems the conversion is damaged. And sometimes perl on a UNIX box can get very upset when it happens.

Normally if you FTP in ASCII mode the conversion is done for you, while BINARY mode will not convert.

If you determine that the returns are causing the problems here are some perl one liners that can help...


Code
#PC to Unix 
perl -pe "s/\r//sig" < Input_File > Output_File

#Mac to Unix
perl -pe "s/\r/\n/sig" < Input_File > Output_File

Yes it is true that the first version will actually work for both, but the resulting file will be without any "\n" line breaks so the whole file will be on one line. The second version is provided in the event you might actually want to read the file on the other end.

If you will be converting several files you can use this longer perl solution...


Code
#!/usr/local/bin/perl -w 

use strict;

my $string;

$/ = "";

foreach (@ARGV) {
open (FILE, $_);
$string = <FILE>;
close FILE;

$string =~ s/\r\n/\n/sig;
$string =~ s/\r/\n/sig;

open (FILE, ">$_");
print FILE $string;
close FILE;
}

$/ is reset so the whole file is on one line, otherwise the default is to break on "\n".

First convert any PC lines from "\r\n" to unix "\n".

Second convert any mac lines from "\r" to unix "\n". Adding the "\n" is not needed for the new file to run, but it is if you want to read it. If you don't put in "\n" the whole file will be on one line.

This program takes filenames from the command line as input. You could use opendir to mangle a whole directory of files if needed.

---
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


S_Shrum
User

Jan 24, 2001, 10:21 AM

Post #5 of 7 (868 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

That seemed to do the trick. Why?

The shebang line was the first code line beneath the comments.

Sean Shrum
<a href="http://www.shrum.net">www.shrum.net</a>


japhy
Enthusiast

Jan 24, 2001, 12:22 PM

Post #6 of 7 (865 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

Useless use of /s and /i modifiers in those regexes.

And you meant to set $/ to undef, not "".

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


rGeoffrey
User / Moderator

Jan 24, 2001, 7:06 PM

Post #7 of 7 (862 views)
Re: Premature end of script headers - involved code [In reply to] Can't Post

That is what I get for grabbing code I wrote long ago.

But I think I would rather write...

local $/;

It still sets it to undef and as I only use local for those special cases, it would help to remind me that the variable is changed if I place this into a larger program.

---
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives