| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
Condition33%1/3
$] < "5.006" and not defined &warnings::import
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006"
not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
| |||||||||||||||||||||||||||||
| 18 | 0 | $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } } | |||||||||||||||||||||||||||
| 19 | 1 | ▶ | use warnings; local $^W = 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use FindBin (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use lib "$FindBin::Bin/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Perl500503Syntax::OrDie (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $banner = '-' x 60; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | # Check a file given on the command line | ||||||||||||||||||||||||||||
| 29 | 0 | my $file = $ARGV[0]; | |||||||||||||||||||||||||||
| 30 | 0 | print "$banner\n"; | |||||||||||||||||||||||||||
| 31 | 0 | print "Checking: $file\n"; | |||||||||||||||||||||||||||
| 32 | 0 | print "$banner\n"; | |||||||||||||||||||||||||||
| 33 | 0 | eval { Perl500503Syntax::OrDie::check_file($file) }; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | print $@; | |||||||||||||||||||||||||||
| 36 | 0 | exit 1; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | print "No violations found.\n"; | |||||||||||||||||||||||||||
| 39 | 0 | exit 0; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Built-in demonstration | ||||||||||||||||||||||||||||
| 43 | 1 | print "Perl500503Syntax::OrDie v$Perl500503Syntax::OrDie::VERSION demo\n"; | |||||||||||||||||||||||||||
| 44 | 1 | print "$banner\n"; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Example 1: clean Perl 5.005_03-compatible code | ||||||||||||||||||||||||||||
| 47 | 1 | 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 | 1 | print "Example 1: valid Perl 5.005_03 code\n"; | |||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 1 | my @v = Perl500503Syntax::OrDie::check_source($clean, 'example1'); | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | if (@v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | print " -> UNEXPECTED violation:\n"; | |||||||||||||||||||||||||||
| 62 | 0 | print " $_\n" for @v; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 1 | print " -> No violations. OK\n"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Example 2: code using 'our' (Perl 5.6 feature) | ||||||||||||||||||||||||||||
| 71 | # (string built at runtime to avoid selfcheck false-positive) | ||||||||||||||||||||||||||||
| 72 | 1 | my $bad_our = 'ou' . 'r $config = {};' . "\n" | |||||||||||||||||||||||||||
| 73 | . 'ou' . 'r @items = (1, 2, 3);' . "\n"; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | print "Example 2: 'our' declaration (Perl 5.6+)\n"; | |||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 1 | my @v = Perl500503Syntax::OrDie::check_source($bad_our, 'example2'); | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (@v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 0 | print " -> (not detected)\n"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Example 3: defined-or-assign (Perl 5.10 feature) | ||||||||||||||||||||||||||||
| 88 | # (string built at runtime to avoid selfcheck false-positive) | ||||||||||||||||||||||||||||
| 89 | 1 | my $bad_defor = 'my $value = undef;' . "\n" | |||||||||||||||||||||||||||
| 90 | . '$value ' . join('', '/', '/') . "= 'default';\n"; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | print "Example 3: defined-or-assign (Perl 5.10+)\n"; | |||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 1 | my @v = Perl500503Syntax::OrDie::check_source($bad_defor, 'example3'); | |||||||||||||||||||||||||||
| 95 | 1 | ▶ | if (@v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||
| 99 | 0 | print " -> (not detected)\n"; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Example 4: say (Perl 5.10 feature) | ||||||||||||||||||||||||||||
| 105 | # (string built at runtime to avoid selfcheck false-positive) | ||||||||||||||||||||||||||||
| 106 | 1 | my $bad_say = 'use feature ' . "'" . 'say' . "'" . ';' . "\n" | |||||||||||||||||||||||||||
| 107 | . 'sa' . 'y "Hello, world!";' . "\n"; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | print "Example 4: 'say' and 'use feature' (Perl 5.10+)\n"; | |||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 1 | my @v = Perl500503Syntax::OrDie::check_source($bad_say, 'example4'); | |||||||||||||||||||||||||||
| 112 | 1 | ▶ | if (@v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | print " -> $_" for map { (my $m=$_)=~s/\s+\z//; "$m\n" } @v; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 0 | print " -> (not detected)\n"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | print "$banner\n"; | |||||||||||||||||||||||||||
| 122 | 1 | print "Demo complete.\n"; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||