premature end of script headers

Discussion in 'Perl Misc' started by Raistlin Majere, May 3, 2006.

  1. 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?
     
    Raistlin Majere, May 3, 2006
    #1
    1. Advertising

  2. Raistlin Majere

    David Squire Guest

    Raistlin Majere wrote:
    > 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
     
    David Squire, May 3, 2006
    #2
    1. Advertising

  3. Raistlin Majere

    Paul Lalli Guest

    Raistlin Majere wrote:
    > print "Content-type: text/html\n\n";
    >
    > open(OLDLIST, "<link list.txt");
    > open(NEWLIST, ">link list.tmp");

    <SNIP>
    > # 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
     
    Paul Lalli, May 3, 2006
    #3
  4. "Raistlin Majere" <> wrote in
    news::

    #!/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:

    while ( my $oldline = <OLDLIST> ) {
    chomp($oldline);

    > 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>
     
    A. Sinan Unur, May 3, 2006
    #4
  5. 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);
     
    Raistlin Majere, May 3, 2006
    #5
  6. "Raistlin Majere" <> wrote in
    news::

    > 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
     
    A. Sinan Unur, May 3, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne Deleersnyder

    Premature end of script headers

    Wayne Deleersnyder, Nov 21, 2003, in forum: Perl
    Replies:
    1
    Views:
    669
    Wayne Deleersnyder
    Nov 21, 2003
  2. Greeshma
    Replies:
    0
    Views:
    351
    Greeshma
    Feb 25, 2004
  3. Greeshma
    Replies:
    1
    Views:
    494
  4. Phil Frost

    Re: premature end of script headers

    Phil Frost, Jul 10, 2004, in forum: Python
    Replies:
    0
    Views:
    547
    Phil Frost
    Jul 10, 2004
  5. Kevin Layman
    Replies:
    3
    Views:
    139
    Diego Guillen
    May 22, 2009
Loading...

Share This Page