in reply to CGI script connecting via terminal but not via browser
Oops, never mind my original answer; I missed the line where you said the actual variables were filled in there.
It would seem there's a difference in the environment when it's run as a CGI by apache. You can compare by writing a simple CGI that reports on the environment and info on perl, like this:
Running that from the terminal and through a browser should help you spot any differences that might affect your script. The perl -v line especially will show if your script could be picking up different modules in either case.
Available for small or large Perl jobs; see my home node.