[Re: Perl Question?]

Matt Graham danceswithcrows@usa.net
16 Oct 2001 15:11:15 EDT


Edward Glowacki <glowack2@msu.edu> wrote:
> Quoted from Jeffrey Utter on Tue, Oct 16, 2001 at 02:21:52PM -0400:
>> 
>>I have a perl script running on my server that takes in part of a url 
>>as it's argument then loads the appropriate page.
> > 
>> Here is an example:   say I want to load  /user/Jeff/page/
>> 
>> well the call to the script looks like script.pl?/user/Jeff/page/
>> This is kind of like a big wrapper to go around any other html code I
>> happen to create so I don't have to type it over and over again.  
>> 
>> anyway the question is:  Is there a way I can have a perl script tell 
>> the webserver that it had a 404 error?  so the server 404 page is 
>> generated as a response.  

> I'm not sure the exactly, but your question prompted me to look up
> the answer to a question I've had for a long time.
> 
> Question:  How do you customize the 404 page? =)
> 
> Answer:  Add something like this to your apache.conf (or httpd.conf
> or whatever it is these days),
> 
> ErrorDocument 404 /usr/local/www/data/error404.html

Jeff:  You need to clarify what you want here.  A Perl script will not "have a
404 error", it will be unable to open a file for reading.  I assume you're
taking the argument passed in from the URL and running a -r on it, so if the
-r comes back FALSE, you should be able to use something from CGI.pl to issue
a redirect that leads to the error document.  I had a bit of badly written
code that did something very similar to this, but I seem to have left it at
home.  Hmm, let's try this.  Warning, this has not been tested and may cause
your Apache to explode:

#!/usr/bin/perl -w
use CGI qw/:standard/;

$query = new CGI;
#initialization, etcetera, do NOT print a header or anything until...
$file_to_get = $query->param;  # you have a name for this param?
if(not -r $file_to_get){
    print $query->redirect('http://myserver.org/error404.html');
    exit 0;
    }
# the script you had before continues here.

Take this with a grain of salt; I haven't used CGI.pm in some time although
I've got a project I'd like to do that really needs the whole CGI treatment. 
The problem is finding enough time to do the artistic rework....

-- 
Matt G / Dances With Crows
There is no Darkness in Eternity/But only Light too dim for us to see
"I backed up my brain to tape, but tar says the tape contains no data...."