in reply to source code of cgi script directly opens in browser

Even though you're just learning cgi, its a good time to start thinking about security. The first thing you should do is be sure that your script directory is outside of your html document tree, and use ScriptAlias as mentioned by ikegami. You might also want to read this node on cgi security.

# this /path/to/apache | +---/cgi-bin | +---/htdocs # not this /path/to/apache | +---/htdocs | +---/cgi-bin # add to httpd.conf ScriptAlias /cgi-bin/ "/path/to/apache/cgi-bin/"