This isn't possible with CGI. The CGI RFC doesn't define any mechanism for determining the order of the headers. Additionally, environment variables in general don't have a defined order, and Perl's %ENV hash is randomized. The only way to do this would be to have a different mechanism of communicating with the server software such as ig's mod_perl example.
It wouldn't be possible to do this in PHP for scripts run as CGI either. Assuming you meant PHP's getallheaders function, it is even documented as only working when running as an Apache module.