http://www.perlmonks.org?node_id=673140


in reply to Populate CGI from HTTP::Request wo/ using %ENV ?

To the best of my knowledge neither CGI nor CGI::Simple give you a way to populate header information directly into their respective objects; they just pull it from ENV automatically. Those are the only two CGI-related modules I've personally worked with; there may be others. A quick experiment shows you can localize %ENV though:
#!perl -l sub foo { local %ENV; $ENV{foo} = '1'; print $ENV{foo}; } foo; print 'yes' if $ENV{foo} eq '1';
doesn't print "yes"

Replies are listed 'Best First'.
Re^2: Populate CGI from HTTP::Request wo/ using %ENV ?
by ikegami (Patriarch) on Mar 09, 2008 at 22:33 UTC

    local %ENV doesn't remove the magic attached to %ENV, so changes to %ENV still affect every thread, so the race condition remains.

    THREAD 1 THREAD 2 ---------------------------------- ------------------------------ +---- { T local %ENV; i -> Process's R_M remains undef m $ENV{REQUEST_METHOD} = 'GET'; e -> Process's R_M becomes GET | | { | local %ENV; v -> Process's R_M becomes undef $ENV{REQUEST_METHOD} = 'PUT'; -> Process's R_M becomes PUT print($ENV{REQUEST_METHOD}); -> Prints process's R_M (PUT) } -> Process's R_M restored to undef print($ENV{REQUEST_METHOD}); -> Prints process's R_M (undef +) } -> Process's R_M restored to u +ndef

    That's why I suggested local *ENV instead. Then modifying %ENV doesn't change the processes environment.

    Update: Added illustration of two race conditions.