To make this short and sweet, you want to look into 'suidperl' or sperl. This allows for the script to be run securly (unless -U is specified) while changing the effective user id of the process it will run under.
You dont really need to set somthing 777 to modify a file, having user write permissions or group write permissions should be the way to go. I dont think making somthing setuid in this case would be a good idea, unless you want multiple users in multiple groups (still you should just add them to a central group that has permission to write to that specified file)to have access to that file.
having somthing setuid is usually somthing that is done for giving rights to things that a normal user shouldnt usually have rights to. (IE icmp using ping or somthing). Though sperl does provide a good basis for "not messing up and keeping it secure", there will always be that mist of air that screams "security hole"