| 1 | #!/usr/bin/env perl | ||
| 2 | # Copyright (C) 2012 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | # Documentation is at the __END__ | ||
| 4 | package Perl::Syntax; | ||
| 5 | our $VERSION = '1.00'; | ||
| 6 | use B qw(minus_c save_BEGINs); | ||
| 7 | sub import { | ||
| 8 | my ($class, @options) = @_; | ||
| 9 | my $filename = $options[0] if @options; | ||
| 10 | open (SAVE_ERR, '>&STDERR'); | ||
| 11 | eval q[ | ||
| 12 | BEGIN { | ||
| 13 | minus_c; | ||
| 14 | save_BEGINs; | ||
| 15 | close STDERR; | ||
| 16 | if (defined($filename)) { | ||
| 17 | open (STDERR, ">", $filename); | ||
| 18 | } else { | ||
| 19 | open (STDERR, ">", \$Perl::Syntax::stderr); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | ]; | ||
| 23 | die $@ if $@ | ||
| 24 | } | ||
| 25 | |||
| 26 | unless (caller) { | ||
| 27 | # Demo code | ||
| 28 | printf "Demoing %s ...\n", __FILE__; | ||
| 29 | require File::Basename; | ||
| 30 | my $dirname = File::Basename::dirname(__FILE__); | ||
| 31 | my $basename = File::Basename::basename(__FILE__); | ||
| 32 | chdir $dirname; | ||
| 33 | my @prefix = ("$^X", '-I..', "-MPerl::Syntax"); | ||
| 34 | system("$^X -I.. -MPerl::Syntax $basename"); | ||
| 35 | system(@prefix, $basename); | ||
| 36 | printf("Perl file $basename does %ssyntax check okay.\n", | ||
| 37 | $?>>8 ? 'not ' : ''); | ||
| 38 | system(@prefix, '-e', '1+'); | ||
| 39 | printf "1+ does %ssyntax check okay.\n", $?>>8 ? 'not ' : ' '; | ||
| 40 | require File::Temp; | ||
| 41 | my ($fh, $tempfile) = File::Temp::tempfile('SyntaxXXXX', SUFFIX=>'.log', | ||
| 42 | UNLINK => 1, | ||
| 43 | TMPDIR => 1); | ||
| 44 | @prefix = ("$^X", '-I..', "-MPerl::Syntax=$tempfile"); | ||
| 45 | system(@prefix, $basename); | ||
| 46 | print "Syntax checking $basename gives message:\n"; | ||
| 47 | seek($fh, 0, 0); | ||
| 48 | print $_ while <$fh> ; | ||
| 49 | |||
| 50 | seek($fh, 0, 0); | ||
| 51 | system(@prefix, '-e', 'not(perl'); | ||
| 52 | print "Syntax checking 'not(perl' gives message:\n"; | ||
| 53 | seek($fh, 0, 0); | ||
| 54 | print $_ while <$fh> ; | ||
| 55 | close $fh; | ||
| 56 | } | ||
| 57 | 1; | ||
| 58 | __END__ |