This snippet works in one of my applications just fine, I think it
meets your requiremeents for the most part. This is from a web-based
login that redirects to the calling script.
in reply to try to send cookie and redirect
Create and set cookie, then redirect.
my $cookie_name = "FOO" . $E . "SID";
my $SID_cookie = new CGI::Cookie(-name => $cookie_name,
-value => $sid,
-path => '/',
-expires => "+2h",
-domain => '.foobar.com',
print "Set-Cookie: " . $SID_cookie . "\n";