File Coverage

eg/check_compatibility.pl
Criterion Covered Total %
statement 16 18 88.8
branch 1 2 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod n/a
total 24 29 82.7


line stmt bran cond sub pod time code
1             ######################################################################
2             #
3             # check_compatibility.pl
4             #
5             # Demonstrates Perl500503Syntax::OrDie programmatic API.
6             # Checks a given Perl source file or inline code for
7             # Perl 5.005_03 compatibility violations.
8             #
9             # Usage:
10             # perl eg/check_compatibility.pl script.pl
11             # perl eg/check_compatibility.pl (runs built-in examples)
12             #
13             # Demonstrates: check_file, check_source
14             #
15             ######################################################################
16 1     1   129866 use strict;
  1         1  
  1         63  
17 1 50 33 1   33 BEGIN { if ($] < 5.006 && !defined(&warnings::import)) {
18 0         0 $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
  0         0  
19 1     1   4 use warnings; local $^W = 1;
  1         1  
  1         54  
20 1     1   5 use FindBin ();
  1         1  
  1         27  
21 1     1   3 use lib "$FindBin::Bin/../lib";
  1         2  
  1         6  
22              
23 1     1   763 use Perl500503Syntax::OrDie ();
  1         2  
  1         474  
24              
25             my $banner = '-' x 60;
26              
27             if (@ARGV) {
28             # Check a file given on the command line
29             my $file = $ARGV[0];
30             print "$banner\n";
31             print "Checking: $file\n";
32             print "$banner\n";
33             eval { Perl500503Syntax::OrDie::check_file($file) };
34             if ($@) {
35             print $@;
36             exit 1;
37             }
38             print "No violations found.\n";
39             exit 0;
40             }
41              
42             # Built-in demonstration
43             print "Perl500503Syntax::OrDie v$Perl500503Syntax::OrDie::VERSION demo\n";
44             print "$banner\n";
45              
46             # Example 1: clean Perl 5.005_03-compatible code
47             my $clean = <<'CODE';
48             use strict;
49             use vars qw($x @items %data);
50             $x = 42;
51             open(FH, ">output.txt") or die $!;
52             print FH "$x\n";
53             close FH;
54             mkdir("newdir", 0755) or die $!;
55             CODE
56              
57             print "Example 1: valid Perl 5.005_03 code\n";
58             {
59             my @v = Perl500503Syntax::OrDie::check_source($clean, 'example1');
60             if (@v) {
61             print " -> UNEXPECTED violation:\n";
62             print " $_\n" for @v;
63             }
64             else {
65             print " -> No violations. OK\n";
66             }
67             }
68             print "\n";
69              
70             # Example 2: code using 'our' (Perl 5.6 feature)
71             # (string built at runtime to avoid selfcheck false-positive)
72             my $bad_our = 'ou' . 'r $config = {};' . "\n"
73             . 'ou' . 'r @items = (1, 2, 3);' . "\n";
74              
75             print "Example 2: 'our' declaration (Perl 5.6+)\n";
76             {
77             my @v = Perl500503Syntax::OrDie::check_source($bad_our, 'example2');
78             if (@v) {
79             print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v;
80             }
81             else {
82             print " -> (not detected)\n";
83             }
84             }
85             print "\n";
86              
87             # Example 3: defined-or-assign (Perl 5.10 feature)
88             # (string built at runtime to avoid selfcheck false-positive)
89             my $bad_defor = 'my $value = undef;' . "\n"
90             . '$value ' . join('', '/', '/') . "= 'default';\n";
91              
92             print "Example 3: defined-or-assign (Perl 5.10+)\n";
93             {
94             my @v = Perl500503Syntax::OrDie::check_source($bad_defor, 'example3');
95             if (@v) {
96             print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v;
97             }
98             else {
99             print " -> (not detected)\n";
100             }
101             }
102             print "\n";
103              
104             # Example 4: say (Perl 5.10 feature)
105             # (string built at runtime to avoid selfcheck false-positive)
106             my $bad_say = 'use feature ' . "'" . 'say' . "'" . ';' . "\n"
107             . 'sa' . 'y "Hello, world!";' . "\n";
108              
109             print "Example 4: 'say' and 'use feature' (Perl 5.10+)\n";
110             {
111             my @v = Perl500503Syntax::OrDie::check_source($bad_say, 'example4');
112             if (@v) {
113             print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v;
114             }
115             else {
116             print " -> (not detected)\n";
117             }
118             }
119             print "\n";
120              
121             print "$banner\n";
122             print "Demo complete.\n";
123