I have 2 perl scripts A and B. inside B.pl I want to check if there are syntax errors in A.pl
I'm using $result =`perl -c A.pl`;
but I can't get the output in $result
.
I need a perl command to do that. can you help?
I have 2 perl scripts A and B. inside B.pl I want to check if there are syntax errors in A.pl
I'm using $result =`perl -c A.pl`;
but I can't get the output in $result
.
I need a perl command to do that. can you help?
You can capture the output and check for the string syntax OK
A.pl
#!/usr/bin/env perl
use warnings;
use strict;
main();
exit 0;
sub main {
my ($stuff) = @_;
# Syntax Error
print $stufff;
# Runtime Error
pint $stuff;
# Runtime Error
print $stuff->foo();
}
B.pl
#!/usr/bin/env perl
use warnings;
use strict;
use Test::More;
for my $file ( qw( A.pl B.pl ) ) {
# 2>&1 means redirect STDERR to STDOUT so we can capture it
chomp(my ($result) = qx( /usr/bin/env perl -cw $file 2>&1 ));
ok( $result =~ m|syntax OK|m, "$file compiles OK" )
or diag( "$file failed to compile : $result" );
}
done_testing();
Output
perl B.pl
not ok 1 - A.pl compiles OK
# Failed test 'A.pl compiles OK'
# at B.pl line 8.
# A.pl failed to compile : Global symbol "$stufff" requires explicit package name at A.pl line 12.
ok 2 - B.pl compiles OK
1..2
# Looks like you failed 1 test of 2.
my $res = system("perl -c test.pl");
unless ($res) {
print "OK\n";
} else {
print "Syntax error!\n";
}