It's probably an error with permissions. Remember, mod_perl processes typically run as user "nobody." Try putting in some debug statements to see where things start to go differently when running under mod_perl.
The CGI version works find (also running as nobody).
The code thinks it sent stuff (ie warns just above the prints produce the correct output) but the other process does not get the data (but does in the cgi or run from shell cases).