Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

mod_perl and CGI::header

by saberworks (Curate)
on Sep 29, 2005 at 23:22 UTC ( [id://496282]=perlquestion: print w/replies, xml ) Need Help??

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

I ran into a frustrating issue today at work. Frustrating because it took me a while to figure out what was going on.

We have an application which runs on mod_perl under a handler called PerlDo. I recently started here so I'm not sure whether PerlDo is a custom handler or released by someone.

Anyway, I was trying to print some arbitrary HTTP headers to communicate with a client application which is not a regular web browser. These headers were something like: "X-error-code"

So inside my "cgi" script (which runs under PerlDo, which in turn runs under mod_perl, so PerlDo seems similar to Apache::Registry), I did this:
print $cgi->header(-X_error_code => 1);
According to the CGI.pm docs, the CGI::header method is supposed to discard the leading dash, turn underscores into dashes, format the header, and return it. So you have to print it.

However, my headers were never sent. In fact, $cgi->header() didn't even return them. I tested a small test script on advice in the chatterbox and it works fine:
perl -MCGI=header -we'print header('-X_error_code' => 1)'
So I dug around in CGI.pm and in the header method I found this code:
if ($MOD_PERL and not $nph) { $self->r->send_cgi_header($header); return ''; }
I'm assuming this means CGI.pm wants to think it is smart enough to dispatch the headers instead of just returning them IF it is running under mod_perl. To me, this seems like an extremely silly assumption (pretend I'm using the formatted headers for something else, not just printing them). Furthermore, even if it did want to do that, apparently passing them to the r->send_cgi_header method isn't working regardless.

And it won't return the actual header.

Will someone please explain this behavior? Thanks in advance.

Replies are listed 'Best First'.
Re: mod_perl and CGI::header
by PodMaster (Abbot) on Sep 30, 2005 at 00:04 UTC
    Consider this
    use CGI 'header'; use strict; print header(-type=>'text/plain'),"hi\nthere\nbuddy";
    The CGI module is designed so that you can take the above script, stick it under Apache::Registry (or equivalent) and it will still work without any modifications.

    If you want the headers to be returned and not sent, $nph needs to be true (-nph =>1).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://496282]
Approved by Crackers2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found