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

Sorry, as this might not technically be a Perl question. I have a script that, under certain circumstances, I want to return a "true" 404 error.

I've tried a few things, and the best I can come up with is:

print "Status: 404 Not Found\n";

This "appears" to work, but doesn't appear to be a true 404 error. That is, my logs don't show it as a 404 error. And my host's 404 page handler is not called.

Does anyone know how to send a true 404 error via Perl?

Replies are listed 'Best First'.
Re: How to send a "true" 404 error via Perl
by ikegami (Patriarch) on Jan 17, 2007 at 22:59 UTC

    Works for me with Apache.

    test.cgi:

    #!/usr/bin/perl print "Status: 404 Not Found\n"; print "\n";

    access log:

    xx.xx.xx.xx - - [17/Jan/2007:17:56:39 -0500] "GET /test.cgi HTTP/1.1" +404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.1 +) Gecko/20061204 Firefox/2.0.0.1" www.example.com

    I can even return an HTML message.

    test.cgi:

    #!/usr/bin/perl print "Status: 404 Not Found\n"; print "Content-Type: text/html\n"; print "\n"; print "<title>404 Not Found</title>\n"; print "<h1>404 Not Found</h1>\n";

    access log:

    xx.xx.xx.xx - - [17/Jan/2007:18:00:15 -0500] "GET /test.cgi HTTP/1.1" +404 52 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1. +1) Gecko/20061204 Firefox/2.0.0.1" www.example.com
      Hm, not for me...
      xx.xx.xx.xx- - [17/Jan/2007:15:02:15 -0800] "GET /crueljoke.pl HTTP/1. +1" 200 692 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1. +8.1.1) Gecko/20061204 Firefox/2.0.0.1"

      And looks as though others (go through google.com to avoid their cloaking) also have this problem.

        The linked case has two problems. The status header didn't come first, and "Status" was misspelled as "status".

        What's your script? Better yet, did you try mine? (Since yours is hardly a minimal test with 692 bytes of content.)

Re: How to send a "true" 404 error via Perl
by shmem (Chancellor) on Jan 17, 2007 at 23:56 UTC
    Is yours a perl script?
    use CGI; print header(-status => 404);

    should do. What is your apache version?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to send a "true" 404 error via Perl
by muba (Priest) on Jan 17, 2007 at 23:28 UTC
    I'm not sure, but don't you usually just go
    404 Not Found\n
    ?
Re: How to send a "true" 404 error via Perl
by DrHyde (Prior) on Jan 18, 2007 at 10:40 UTC
    Sometimes (I've never tracked down exactly why) Apache ignores some of the headers emitted by CGI scripts. The solution is to name the script nph-.... NPH stands for Non-Parsed Headers.

    In older versions of Apache using nph scripts was also the only way to get an unbuffered connection.