doorslam has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a quick script to check a set of files before committing them to cvs. I would like to use the perl -c command on each script in the directory, but am not sure how to parse the response to know if there were errors in the file it was checking. I am currently using this:
$rc = `$^X -c $_`; chomp $rc; unless ($rc eq "$_ syntax OK") { print "$rc"; $errors++; }

I seem to be getting back an $rc of "$_ syntax OK\n" even after the chomp, however, for when it prints the $rc, it still has a newline.

Thanks for any suggestions,
B.
Catapulta habeo. Nisi pecuniam omnem mihi dedis, ad caput tuum saxum immane mittam.

Replies are listed 'Best First'.
Re: cvs control/perl -c
by jmcnamara (Monsignor) on Feb 04, 2002 at 13:20 UTC

    Your problem is that the output from perl -c is going to STDERR. Try this if your shell supports this type of redirection:
    my $rc = `$^X -c $_ 2>&1`; chomp $rc; unless ($rc eq "$_ syntax OK") { print "$rc"; $errors++; }
    This is explained in qx/STRING/ in perlop.

    Otherwise you could examine the value of $? after using the backticks, see perlvar:

    my $rc = `$^X -c $_`; if ($?) { print "$rc"; $errors++; }
    --
    John.

(tye)Re: cvs control/perl -c
by tye (Sage) on Feb 04, 2002 at 15:15 UTC

    Why not just check whether or not "perl -c" exits with a non-zero status value?     if(  0 != system($^X,"-c",$_)  )

            - tye (but my friends call me "Tye")
Re: cvs control/perl -c
by gellyfish (Monsignor) on Feb 04, 2002 at 13:18 UTC

    It might be that you have something other than the value of $/ at the end of the line - what OS are you running this on ? You could determine what is on the end of the line by redirecting the output to a file and examining it with od or its moral equivalent.

    Of course it is only the 'Syntax OK' message that is going to STDOUT - as jmcnamara points out any error messages will go to STDERR so you should arrange for that to be redirected

    You also might want to consider examining $? rather than comparing the output string so your test would become :

    if ( $? ) { print $rc; $errors++; }

    /J\

      running on Linux with perl v5.6.0...

      Thanks for the help, it's working properly now. I'm using the "if ( $? )" method so that the OK message is still dropped to stdout just for some feedback to the user.

      B.
      Catapulta habeo. Nisi pecuniam omnem mihi dedis, ad caput tuum saxum immane mittam.
        Small quibble: all output from perl -c goes to STDERR, whether it's "syntax OK" or error messages.