While the advice about agents verses daemons is generally good, this is clearly a test setup being done by the OP. If you check his other threads, his ultimate goal server-wise does fit the bill of daemon, it won't work as an agent.
However, the advise of not running as root is valid. You should follow the model of likes of Apache and MySQL. Create a special-purpose, non-root user for the purposes of running your sever. Have all the files owned by this special-purpose user.
In regards to launchd troubles, the files below do work.
/var/tmp/a/test.plist
Code:
$str ="Test Daemon !....";
open FILE, ">file.txt" or die $!;
print FILE $str;
close FILE;
exit -1; # Don't normally do this, see below.
/Library/LaunchDaemons/local.service.test.plist
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.service.test</string>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<true/>
</dict>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/perl</string>
<string>/var/tmp/a/test.pl</string>
</array>
<key>StandardErrorPath</key>
<string>/var/tmp/a/test.err</string>
<key>StandardOutPath</key>
<string>/var/tmp/a/test.out</string>
<key>WorkingDirectory</key>
<string>/var/tmp/a</string>
</dict>
</plist>
A word first about the exit -1. The exit -1 in the perl script is only for this test. You would normally
not do this. The script launched by launchd is meant to keep running while your server is running. If the script exits, it means the server exited and needs to be relaunched. But this test script exits very quickly. The exit -1 causes an unsuccessful exit, and stops launchd from relaunching the script over and over rapidly.
The primary reason your script failed to run was because you'd coded an on-demand launchd plist file. But you never specified under what conditions the test script should be run, so it never did. You need to include the KeepAlive key like above. This particular KeepAlive setup also implies RunAtLoad, so launchd will immediately run the test script.
Even if you'd got that right, the running of the script would have failed. At no point did you setup perl to run the script. A .pl extension is not enough. You either need a she-bang line at the top of the script, or to explicitly invoke perl like I've done in the .plist file.
Lastly you use a relative path for file.txt in your test script, but what do you expect the working directory to be? You've got to set the in the .plist file.
EDIT: I seems you have very little Unix experience. Writing a server under Mac OS X without Unix server administration experience is going to be bad. I'd suggest you learn how to install, setup and administer a Unix server package such as Apache or MySQL. In doing so you will learn how a Unix server is meant to behave, and give you a model for creating your own server.