premature end of script headers

  • Thread starter Raistlin Majere
  • Start date
R

Raistlin Majere

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

open(OLDLIST, "<link list.txt");
open(NEWLIST, ">link list.tmp");

$count=1;

$line1="";
$line2="";

foreach $line(<OLDLIST>)
{
chomp($line);

if($count==1)
{
$line1=$line;
};

if($count==2)
{
$line2 =~ s/Suggested Text/$line1/;

print NEWLIST $line2."\n";
};

if($count==3)
{
$count=0;
};

$count++;
};

close(NEWLIST);
close(OLDLIST);

# what is wrong?
 
D

David Squire

Raistlin said:
print "Content-type: text/html\n\n";
....

For a start, what is in the error log on the web server that reported
"premature end of script headers". That should give you a clue as to
what went wrong.

After that, you script has:

- no shebang line (#!/usr/bin/perl)
- no "use strict;"
- no "use warnings;"
- an unquoted (\Q) variable in a substitution
- if's that should be elsif's

Still, my first guess would be the lack of a shebang line. Can your web
server (Apache?) even find perl, or know that this is a Perl script?

DS
 
P

Paul Lalli

Raistlin said:
print "Content-type: text/html\n\n";

open(OLDLIST, "<link list.txt");
open(NEWLIST, ">link list.tmp");
# what is wrong?

What happened when you tried to run this program on the command line,
by just typing the path to the file (ie, not preceding it with 'perl')
? Did it work as you expect? (Hint: NO, it did not.) Why not?

Paul Lalli
 
A

A. Sinan Unur

#!/usr/bin/perl

use strict;
use warnings;

missing
print "Content-type: text/html\n\n";

open(OLDLIST, "<link list.txt");

Always, yes, always check if the open call succeeded.

What do you think this call is supposed to do?
open(NEWLIST, ">link list.tmp");
Ditto.


$count=1;

$line1="";
$line2="";

foreach $line(<OLDLIST>)

By using the for loop, you have already slurped the whole file. Then you
go and process it line-by-line. Do it right from the get go:

if($count==1)
{
$line1=$line;
};

Perl already has a builtin variable keeping track of the line count.
Read perldoc perlvar for $.
if($count==2)
{
$line2 =~ s/Suggested Text/$line1/;


Where is $line2 initialized?
print NEWLIST $line2."\n";
};

if($count==3)
{
$count=0;
};

$count++;
};

close(NEWLIST);
close(OLDLIST);

# what is wrong?

Many things. Make sure that the program works from the command line
before trying it as CGI.

I think this is what you are trying to do:


#!/usr/bin/perl

use strict;
use warnings;

my ( $suggestion );

while ( <DATA> ) {
chomp;
if ( $. % 2 ) {
$suggestion = $_ if $. % 2;
} else {
s/Suggested Text/$suggestion/g;
print $_, "\n";
}
}

__DATA__
read
I will Suggested Text the posting guidelines
help
Posting guidelines Suggested Text me Suggested Text myself,
help
and Suggested Text others Suggested Text me.

C:\Home\asu1\src> perl t.pl
I will read the posting guidelines
Posting guidelines help me help myself,
and help others help me.

C:\Home\asu1\src>
 
R

Raistlin Majere

Thank you all, but I solved the problem. I was trying to write...

#!C:\Programas\xampplite\perl\bin\perl.exe

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

open(OLDLIST, "<link list.txt");
open(NEWLIST, ">link list.tmp");

$count=1;

$text="";
$link="";

foreach $line(<OLDLIST>)
{
chomp($line);

if($count==1)
{
$text=$line;
};

if($count==2)
{
$_ = $line;

s/Suggested Text/$text/;

$link = $_;

print NEWLIST $link."\n";
};

if($count==3)
{
$count=0;
};

$count++;
};

close(NEWLIST);
close(OLDLIST);
 
A

A. Sinan Unur

Thank you all,

Thank us for what? Please quote some context.
but I solved the problem.

What problem did you solve?
I was trying to write...

#!C:\Programas\xampplite\perl\bin\perl.exe

That was only a minor issue.

The whole program is a problem. At this rate, you will cause many, many
headaches to a great many people.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top