What are the best practices for handling file permissions and directory access when integrating Perl scripts with PHP on a web server?

When integrating Perl scripts with PHP on a web server, it is important to ensure that file permissions and directory access are properly managed to maintain security and prevent unauthorized access. One best practice is to set appropriate permissions on files and directories to restrict access to only necessary users and groups. Additionally, using PHP's built-in functions like `file_exists`, `is_readable`, and `is_writable` can help check permissions before accessing files.

// Check file permissions before accessing
$file_path = '/path/to/file.txt';

if (file_exists($file_path) && is_readable($file_path)) {
    // Read file contents
    $file_contents = file_get_contents($file_path);
    
    // Execute Perl script with file contents
    exec("perl script.pl $file_contents");
} else {
    echo "File does not exist or is not readable.";
}