How can PHP distinguish between executing a Perl script as a CGI script and as a command-line script?

PHP can distinguish between executing a Perl script as a CGI script and as a command-line script by checking the value of the $_SERVER['REQUEST_METHOD'] variable. If the value is set to 'GET' or 'POST', it indicates that the script is being executed as a CGI script. If the value is not set or is empty, it indicates that the script is being executed as a command-line script.

if(isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST')) {
    // Execute Perl script as a CGI script
} else {
    // Execute Perl script as a command-line script
}