CandymanCID has asked for the wisdom of the Perl Monks concerning the following question:

I have a little script here, my very first after the 'Hello World' one. Its driving me nuts, I probable have about 10,000 errors and lots of things that could be done better but the one that concerns me is this:

Premature end of script headers: /home/mywebplace/public_html/cgi-bin/populateapp.cgi

I just cant get it to populate the pfd (yeah not my choice either). I have 2 other scripts to take the filled form and put it into SQL, and one to browes the SQL to pick the one to repopulate (for printing) and they both work fine, its repopulating that is giving me a hard time. Here is my miserable attempt at perl.
#!/usr/bin/perl use CGI qw/:standard/; use Acrobat::FDF; use DBI; $data = new CGI; my $tablenamesent = $data->param('tablename'); my $brokerclientsent = $data->param('brokerclient'); my $dbh = DBI->connect("DBI:mysql:mywebplace:localhost","myuser","mypa +ssword"); $sth = $dbh->prepare("SELECT * FROM $tablenamesent WHERE BrokerClient += ?"); $sth->execute($brokerclientsent); my($AmmountOfMortgage, $AmmountOfPurchase, $AttachCopyOf1003, $AttachCopyOfContract, $AttachCopyOfMortgageCommitment, $AttachCopyOfPresentOwnerOrTitlePolicy, $BinderNeededDate, $BrokerClient, $ClosingDate, $CopiesLenderLendersAttorney, $CopiesPurchaserBorrower, $CopiesPurchasersBorrowersAttorney, $CopiesSellerSellersAttorney, $FBByEMail, $FBByFax, $FBByMail, $FBLenderLendersAttorney, $FBPurchaserBorrower, $FBPurchasersBorrowersAttorney, $FBSellerSellersAttorney, $GlobalSalesRep, $LenderAdderss, $LenderCity, $LenderFaxNumber, $LenderName, $LenderPhoneNumber, $LenderState, $LenderZip); $sth->bind_columns(undef, \$AmmountOfMortgage, \$AmmountOfPurchase, \$AttachCopyOf1003, \$AttachCopyOfContract, \$AttachCopyOfMortgageCommitment, \$AttachCopyOfPresentOwnerOrTitlePolicy, \$BinderNeededDate, \$BrokerClient, \$ClosingDate, \$CopiesLenderLendersAttorney, \$CopiesPurchaserBorrower, \$CopiesPurchasersBorrowersAttorney, \$CopiesSellerSellersAttorney, \$FBByEMail, \$FBByFax, \$FBByMail, \$FBLenderLendersAttorney, \$FBPurchaserBorrower, \$FBPurchasersBorrowersAttorney, \$FBSellerSellersAttorney, \$GlobalSalesRep, \$LenderAdderss, \$LenderCity, \$LenderFaxNumber, \$LenderName, \$LenderPhoneNumber, \$LenderState, \$LenderZip); $outFdf = new Acrobat::FDF; $outFdf->SetFile('http://www.mywebplace.com/fdf_pdfs/App.pdf'); $outFdf->SetValue("LenderZip", $LenderZip, 0); $outFdf->SetValue("BrokerClient", $BrokerClient, 0); $outFdf->SetValue("GlobalSalesRep", $GlobalSalesRep, 0); $outFdf->SetValue("LenderState", $LenderState, 0); $| = 1; print "Content-type: application/vnd.fdf\n\n"; $outFdf->SaveToFile('-');
Thanks in advance for your wisdom.

20040330 Edit by castaway: Changed title from 'A Noob with heart...'

Replies are listed 'Best First'.
Re: Premature end of script headers
by Happy-the-monk (Canon) on Mar 29, 2004 at 16:20 UTC

    You will find the real errors in the web server's error_log - have a look!

    Also try running it on the command line. Then you should see the errors on the screen. Best tested on the target system, as the same user as the web server is running with.

    Cheers, Sören

      much agreed about the command line and web server logs... they will help your debugging process.

      Grygonos
Re: Premature end of script headers
by Grygonos (Chaplain) on Mar 29, 2004 at 16:24 UTC
    Welcome to the monestary!

    First off, you need to put

    use strict; use warnings;
    at the top of your script. see strict and warnings for information on what these do. Basically it keeps you from making silly errors that we as developers tend to make. <edit>You'll need to run the script on the command line to see the errors produced. Or as matija said you can carp the fatals to your browser window.<edit>

    Secondly, I don't know anything about Acrobat::FDF and I can't find it on CPAN, but I would wager it has some sort of error string to see if anything is going wrong there.

    I don't see why it would be complaining about pre-mature end of script headers right now.... drop strict and warnings in and see if that sheds any light on it... repost your code once you have it working (back to it's current error msg) with strict and warnings pragma.

    Hopefully you'll find some help in the above babbling.

    Grygonos
Re: Premature end of script headers
by hardburn (Abbot) on Mar 29, 2004 at 16:29 UTC

    It's easier to see errors if you put a use strict; and use warnings; at the top of your code.

    Where did you get the Acrobat::FDF module? It's not on CPAN.

    You should note that the order that data comes out of the database is not guarenteed unless you explicitly tell it otherwise. This means you should avoid using SELECT * (except for interactive use) and instead explicitly write the columns you want, such as SELECT id, ammount, copy, [REST_OF_COLUMNS] [REST_OF_STATEMENT].

    Instead of printing the content type yourself, have CGI.pm generate it: print $data->header( content_type => 'application/vnd.fdf' );.

    Is this really your first non-trivial Perl code? It's one of the cleanest newbie programs I've seen.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      I was curious about that too. I initially assumed that the OP had a typo and meant PDF, not FDF, but a quick google search of Adobe came up with this toolkit. Apparently you have to be registered to get it though.

      There is no emoticon for what I'm feeling now.

Re: Premature end of script headers
by matija (Priest) on Mar 29, 2004 at 16:25 UTC
    You might be getting that error because your program sends an error message before it sends the "Content-type" string.

    You could try moving the print closer to the start of the program, but even better would be

    use CGI::Carp qw/fatalsToBrowser/;
    That should make all the error messages show in your browser window.

Re: Premature end of script headers
by Plankton (Vicar) on Mar 29, 2004 at 16:29 UTC
    Hi Candyman,

    I am not sure why you are doing this ...
    print "Content-type: application/vnd.fdf\n\n";
    Are you trying to force the client to download or execute a fdf file?

    Also you might want to do this to ...
    use strict; use CGI; use CGI::Carp qw /fatalsToBrowser/;
    ... although it might not be a issure here "use strict" and Carp will make your life easier in the long run. Also once you get your issues worked out you might want to do this ...
    #!/usr/bin/perl -wT
    If you want make your cgi more secure.

    Plankton: 1% Evil, 99% Hot Gas.
      Plankton

      print "Content-type: application/vnd.fdf\n\n";

      This line (in theory) tells the browser that what is about to come is a fdf file for a application. On a computer that has Acrobat reader installed it should reconize it as such and fire up the reader in the web browser.
        Well assuming there isn't some other problem couldn't you simply redirect to the URL ...

        http://www.mywebplace.com/fdf_pdfs/App.pdf

        ... after you created it?

        Plankton: 1% Evil, 99% Hot Gas.
Re: Premature end of script headers
by Koosemose (Pilgrim) on Mar 30, 2004 at 00:32 UTC

    Just an off the hip idea, but are you absolutely certain that the modules (particularly Acrobat::FDF) exist on your server? If not, then any of them spitting out an error message of any sort would cause the error you see of premature end of script headers.

    If they do exist, then perhaps there's some other error in your script, that print's out an error message of some sort or another, thereby causing the error you see. You may wish to move your header printing to as soon a point as possible in your script, maybe even in a BEGIN block, (but you'd have to ask other monks for assistance on that, as I'm a little fuzzy on the specifics.)

    Good luck.

    Just Another Perl Alchemist
      Yeah, Im certin its on the server. Under the apache installed perl modules it shows installed. I got the script to the point of actualy opening up acrobat reader, but reader states that the FDF is corrupted. I will post the code as is tomorrow if i dont get it going. Were getting close,dont give up on me now!!!
        Opp last was me...
Re: Premature end of script headers
by CandymanCID (Initiate) on Mar 29, 2004 at 19:54 UTC
    Ok put in all those sugestions...no go. Even with carp, im getting internal server error. That error I posted before is from Apache error log, dunno if thats the same. The FDF type is available from Acrobats web site :-p As for running from command thats impossible as I am running this on a remote server that wont allow access to command line. I have a Debian box here on a Sparc, but no idea how to install the fdf module myself (new to linux too)...Thanks for all your help, I will not give up on this and will post the solution when i find it.