I'm going to have to say "no, it's not possible."
I did some experimenting with making Perl scripts switch users during execution, and didn't have very good results. What I got was unpredictable. If I tried running a command such as this:
Sometimes it would bring the terminal back to a new bash prompt for test2, and the original Perl script that called that command would block until I 'exit' out of that login shell; effectively, Perl wasn't able to execute commands after switching users.
Other times it would just fail altogether and not even try; just the Perl script would quit processing when it got to that command.
I've tried various different syntaxes for calling su, using system(), backticks... I even tried threading it. No luck. And, Perl doesn't have a built-in command to switch user, so it's not looking too good.
So if you want one Perl script to be able to manipulate files owned by two different users, you'll have to run the script as somebody with the power to do so.
One idea might be, if you edit the sudoers file and create a user just for your Perl script and allow it to use sudo without a password (you'd probably want to limit the commands it's allowed to run also), you might be able to have your script just execute specific commands as root.
It wouldn't be very pretty. Perl built-ins wouldn't work with sudo, you'd have to use all system calls.
Code:
system ("sudo chown nobody:nobody myfile.pl");
system ("sudo chmod 0755 myfile.pl");
However, that's a dodgy area as well. It would make scripting a lot more difficult. For example, the Perl script itself might not be allowed to do a "chdir" to a directory owned by another user, so you'd use "sudo chdir" instead... but even so, Perl wouldn't be able to directly work with files it's not allowed to use.
So you couldn't use open() on a file you don't have permission to... you'd need to system("sudo cat ./file");
Messy, I know, but that's the best you can do.
Just run the script as root to begin with and you should be fine.
-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog