in reply to Best way to write to a file owned by root?

but then started getting errors (root doesn't know where my personal perl library resides).

let me suggest a different direction to solve that

i have a file in /usr/bin called perlhuck. It is chmod ugo+x. it contains

#!/bin/sh perl -I /home/huck/cvs/perl_site_lib2 "$@"
where /home/huck/cvs/perl_site_lib2 is my personal perl library. Im very old school, for decades when i wanted to run a perl program id say perl <program>.pl, so it is very natural to say perlhuck <program>.pl too. Having this script around allows me to place this line into my cgi-bin programs as the first line.
#!/usr/bin/perlhuck
And they run just fine knowing where my personal libs are. I also place that as the first line of my chmod ugo+x perl programs. If i wanted to i could extend the path and do all sorts of other things in perlhuck too.

Part of my motivation for perlhuck also come from working in production environments where besides a perllib and/or path i need to set other env variables to insure my process ran correctly.

over time i have written dozens of scripts like this, each designed to set up the correct environment before running some production script.

if you had such a script, say perlnysus then you could say sudo perlnysus <program>.pl and everything would still work fine.

So i suggest you create a perlnysus script, and either say sudo perlnysus <program>.pl or place #!/usr/bin/perlnysus into the first line of your executable perl programs and say sudo <program>.pl