Content with Style

Web Technique

Perl validation from PHP

by Pascal Opitz on February 26 2009, 11:07

A quick method to validate perl scripts from PHP. Took me a long time that perl-c does not use stdout but stderr as output stream ...


public function validationErrorPerl($content) {
  $hash = md5(microtime() . $content);
  $infile = CACHE_DIR . '/validation_' . $hash . '.pl';
  $outfile = CACHE_DIR . $this->config->cache->dir . '/validation_' . $hash . '.txt';
  file_put_contents($infile, $content);
  $cmd = "/usr/bin/perl -c $infile 2> /dev/null &> $outfile";
  exec($cmd);
  $result = file_get_contents($outfile);
  unlink($infile);
  unlink($outfile);
  $result = str_replace($infile, 'perl script', $result);

  if(strpos($result,'syntax OK') !== false) {
    return false;
  }

  return $result;
}