Whats going on here is that both the parent and the child are executing everything below the "if($pid == 0)" point. When you call fork the current process is copied. That means everything you want to execute or not will end up in the child process. I would recommend reading perlipc or looknig at the examples in the perl cookbook. To keep in line with your coding try.
$pid=fork();
if($pid == 0) {
# This is the child
} else {
# This is the parent
}