title: How to write a file with the permissions of the owner in perl
body: <p>I am writing a script and I need to be able to write additional configuration information to a <code>.conf</code> file.</p>
  
  <h3>The Issue:</h3>
  
  <p>The issue is I am unable to write additional data to a file due to a permission issue. Here is the code I am using:</p>
  
  <pre><code>my $conf = "/etc/lighttpd/lighttpd.conf";
  open my $handle, "&gt;&gt;", $conf or die $!;
  # spacing counts, hence the indentation
  print $handle 'fastcgi.server = (
      ".php" =&gt; ((
          "bin-path" =&gt; "/usr/bin/php-cgi",
          "socket" =&gt; "/tmp/php.socket"
      ))
  )'."\n";
  close $handle;
  </code></pre>
  
  <p>Since the <code>lighttpd.conf</code> file is under the umbrella of <code>root:root</code>, I'm unable to write it without doing something along the lines of:</p>
  
  <pre><code>system("sudo chown $USER:$USER /etc/lighttpd/lighttpd.conf");
  # write to file...
  system("sudo chown root:root /etc/lighttpd/lighttpd.conf");
  </code></pre>
  
  <p>The above seems rather hackish to me and I'd rather avoid this.</p>
  
  <h3>The Question</h3>
  
  <p>Are there more elegant ways to go about writing this information to the file? Perhaps a BASH command I am unaware of?</p>