in reply to How to write this code correctly ??? Please help
first: use strict and/or use warnings are two recommended pragmas that you should put on the head of all your scripts you write. i think i do not need to explain why ;)
then consider to use the CGI.pm package. the parameter passing for example is very straightforward with it (have a look at the function Vars).
you should unlock the file at the end of the script. locks are AFAIK a set bit inside the inode of the file.
i do not know what exactly dienice () does - perhaps you should use CGI::Carp qw(fatalsToBrowser); at the top of the cgi; fatal errors become visible in your browser with it.
hope that helped a bit.