OK, here is the version of your script I placed on a Sun Solaris web server:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Cookie;
my $q = new CGI;
my $cookie = $q->cookie(-name => "CUSTOMER1",
-value => "WILE E. COYOTE",
-Path => "/");
my $cookie2 = new CGI::Cookie(-name => "CUSTOMER2",
-value => "BEEP BEEP",
-Path => "/");
print $q->header;
print $cookie;
print "<br>\n"; # $q->br;
print $cookie2;
print "<br>\n"; # $q->br;
(Note: we are using 2.42, the br method is not supported.)
When I run this from a shell command line I get:
Content-Type: text/html
CUSTOMER1=WILE%20E.%20COYOTE; path=/<br>
CUSTOMER2=BEEP%20BEEP; path=/<br>
When I invoke this via a browser, it displays:
CUSTOMER1=WILE%20E.%20COYOTE; path=/
CUSTOMER2=BEEP%20BEEP; path=/
This is in keeping with behavior I would expect.
I am not clear on what it is you are trying to do with this script in the first place. If you are trying to see the actual ASC string that the browser uses to set a cookie, this is the wrong way to do it.
Regardless of which method you are using to create a cookie, all you are doing is creating a reference to a hash with the values needed to make a browser set the cookie when it gets it. This does not include the cookie header or the way the values must be formatted for the browser to handle them properly.
To make a browser set a cookie, you always have to issue the Set-cookie: header prior to the first Content-type: header sent to the browser. Using CGI.pm, this is accomplished by passing the cookies to the header method. If you replace all the print statements in your script with
print $q->header(-cookie => [$cookie, $cookie2]);
print "Hello there!<br>\n";
then the browser will set the cookies properly (and you won't be issuing an empty HTML document either, as your example does).
For further information on cookies, see:
http://wp.netscape.com/newsref/std/cookie_spec.html.
Note that you can bypass CGI.pm altogether and make a CGI script send what the browser needs to set a cookie and display some HTML this way:
#!/usr/bin/perl
print <<EOF;
Set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-
+03 23:12:40 GMT
Content-type:text/html
<html><body>
Here is some HTML blather.
</body></html>
EOF
exit(0);
|