Actually, no.

Sorry, but that doesn't work. I've just tried the following script:

#!/usr/bin/perl use warnings; use strict; print "Location:http://www.perlmonks.org/\n\n";

And it failed - no redirection, just the string "Location:http://www.perlmonks.org/\n\n" in my browser.

The reason it failed on my machine is because (IIRC) Apache looked at the first few lines of output of the script, didn't see a header, and so added the header itself (specifying plain text) before sending the output of the script.

You can make yours work by adding the line

print "HTTP/1.1 302 Moved\n";

above the existing print() line.

I've also just tried the following:

#!/usr/bin/perl use warnings; use strict; use CGI; my $q = new CGI; print $q->redirect( -uri => 'http://www.perlmonks.org/' );
Which outputs:
Status: 302 Moved location: http://www.perlmonks.org/
When called on the command line.

When called by the webserver, it outputs a valid, correct, and working header that redirects the client.

I'm not completely against printing the headers yourself for such a simple task, but I am against printing headers wrongly. Using the CGI module saves you from having to read the RFCs to find out why your script didn't work, and will save time: it took about 5 seconds to produce the CGI.pm method, and closer to 5 minutes for me to make your version work1

When there's a monastery full of monks suggesting 'use CGI;', perhaps they're onto something

Please Note:
This isn't intended to be a telling-off: I did honestly want to see if your version worked.
Cheers.


1: - I'm not good at creating HTTP headers, so I don't claim that to be a good comparison.
davis
Is this going out live?
No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

Update: After an interesting ChatterBox discussion with true, and some playing around with webserver configs, I've come to the following conclusion (for apache):

true's solution works, if:

  1. You're not using modperl, or
  2. You are using modperl, but you've got "PerlSendHeader On" in your httpd.conf, or
  3. You play around with it to add the correct HTTP headers yourself

The CGI.pm solution works whatever because it checks to see if modperl's running, and whether the header's been sent.

References: the modperl guide, and Writing Apache Modules with Perl and C.


In reply to Re: Re: Calling a cgi with another cgi by davis
in thread Calling a cgi with another cgi by Donnie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.