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

I'm confused. I'm writing a cgi script. I know that if I print from my script without first printing the header type, my web broswer will say that there is an internal server error. However, it I print from within a function called from the main body of my script (and I don't print out the header) everything printed displays fine within my web broswer. Why is this?

Replies are listed 'Best First'.
Re: "print" in a CGI script.
by vek (Prior) on Jan 08, 2003 at 18:24 UTC
    shivapd, one of the first things you could do to help us out would be to post some code. Then, once we've had a chance to see what you're doing, we can offer suggestions.

    -- vek --
      Here is a code example as requested.

      The print statements in LDAPerror work fine. They display information in my web browser. I don't know why this works since I'm not printing the header anywhere. If I put a print statement in the main body of my code, I get an internal server error (as I should).

      Thanks,
      Shiva

      #!/usr/bin/perl use CGI; use Net::LDAP; use Net::LDAP::Entry; use Net::LDAP::Util qw( ldap_error_name ldap_error_text); #prepare entry $entry = Net::LDAP::Entry->new(); $addinfo = CGI->new(); my $index = 5000; my($sec, $min, $hour, $day, $mon, $year, $wday, %yday, $isdst) = local +time; $mon += 1; $year += 1900; $dn = "index=" . $index . ", o=cert"; $entry->dn($dn); $entry->add('objectclass' => 'certentry', 'index' => $index, 'openedOn' => $year . $mon . $day . '000000', ); #bind to server $ldap = Net::LDAP->new("shiva.mydomain.com", port => 389) || die("Failed to connect to LDAP host.\n"); $mesg = $ldap->bind("cn=root", password => "root", version => 3); LDAPerror("Bind", $mesg); #add entry to server $mesg = $ldap->add($entry); LDAPerror("Adding", $mesg); #unbind $ldap->unbind; sub LDAPerror { my ($from,$mesg) = @_; print "\n$from\nReturn code: ",$mesg->code ; print "\tMessage: ", ldap_error_name($mesg->code); print " :", ldap_error_text($mesg->code); print "MessageID: ",$mesg->mesg_id; print "\tDN: ",$mesg->dn; print "\n"; #--- # Programmer note: # # "$mesg->error" DOESN'T work!!! # #print "\tMessage: ", $mesg->error; #----- }
        The key is the "\n" before you print anything out in the error subroutine. When the web server looks at this, it usually assumes that the output is text and will print the header for you. For example, the following script will work:
        print "\nStuff!";

        whereas the following will not:
        print "Stuff!";

        You may verify the header business by opening a telnet session to port 80 on your web server and typing:
        GET /path/to/script.pl HTTP/1.0
        and then pressing Enter twice.
Re: "print" in a CGI script.
by sauoq (Abbot) on Jan 08, 2003 at 18:42 UTC

    Odds are about 50/50 that either you are printing the header and don't realize it or it's a get request and your browser has the data cached.

    -sauoq
    "My two cents aren't worth a dime.";