in reply to "print" in a CGI script.

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 --

Replies are listed 'Best First'.
Re: Re: "print" in a CGI script.
by shivapd (Initiate) on Jan 08, 2003 at 19:39 UTC
    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.