File Coverage

blib/lib/Syntax/Check.pm
Criterion Covered Total %
statement 74 77 96.1
branch 30 42 71.4
condition 4 6 66.6
subroutine 14 15 93.3
pod 2 2 100.0
total 124 142 87.3


line stmt bran cond sub pod time code
1             package Syntax::Check;
2              
3 3     3   287573 use warnings;
  3         25  
  3         102  
4 3     3   17 use strict;
  3         5  
  3         65  
5 3     3   18 use feature 'say';
  3         5  
  3         284  
6              
7 3     3   20 use Carp qw(croak confess);
  3         4  
  3         164  
8 3     3   675 use Data::Dumper;
  3         6967  
  3         175  
9 3     3   19 use Exporter qw(import);
  3         6  
  3         138  
10 3     3   22 use File::Path qw(make_path);
  3         7  
  3         195  
11 3     3   760 use File::Temp qw(tempdir);
  3         22036  
  3         182  
12 3     3   1548 use Module::Installed qw(module_installed);
  3         3771  
  3         211  
13 3     3   1599 use PPI;
  3         340974  
  3         442  
14              
15             our $VERSION = '1.05';
16              
17             my $SEPARATOR;
18              
19             BEGIN {
20 3 50   3   36 if ($^O =~ /^(dos|os2)/i) {
    50          
21 0         0 $SEPARATOR = '\\';
22             } elsif ($^O =~ /^MacOS/i) {
23 0         0 $SEPARATOR = ':';
24             } else {
25 3         2206 $SEPARATOR = '/';
26             }
27             }
28              
29             sub new {
30 7     7 1 42692 my ($class, %p) = @_;
31              
32 7 100 66     249 if (! exists $p{file} || ! -f $p{file}) {
33 1         198 croak "new() requires a file name as its first parameter";
34             }
35              
36 6         51 my $self = bless {%p}, $class;
37              
38 6         51 return $self;
39             }
40             sub check {
41 6     6 1 34 my ($self) = @_;
42              
43 6         221 my $doc = PPI::Document->new($self->{file});
44              
45 6         37793 my $includes = $doc->find('PPI::Statement::Include');
46              
47 6         7712 for my $include (@$includes) {
48              
49 34         207 my $module = $include->module;
50 34         999 my $package = $module;
51              
52 34 100       106 if ($module eq lc $module) {
53             # Skip pragmas
54 12 100       256 say "Skipping assumed pragma '$module'" if $self->{verbose};
55 12         43 next;
56             }
57              
58 22 50       76 next if $module =~ /^Carp/;
59              
60 22         123 $module =~ s|::|/|g;
61              
62 22 100       167 if (my ($dir, $file) = $module =~ m|^(.*)/(.*)$|) {
63 20         38 $file .= '.pm';
64 20         55 my $path = "$dir/$file";
65              
66 20 100       94 if (module_installed($package)) {
67             # Skip includes that are actually installed
68 5 100       211 say "Skipping available module '$package'" if $self->{verbose};
69 5         19 next;
70             }
71             else {
72 15         4993 $self->_create_lib_dir;
73              
74 15 50       408 if (! -d "$self->{lib}/$dir") {
75             # Create the module directory structure
76 15 50       3087 make_path("$self->{lib}/$dir") or die $!;
77             }
78              
79 15 50       387 if (! -f "$self->{lib}/$path") {
80             # Create the module file
81 15 50       1006 open my $wfh, '>', "$self->{lib}/$path" or die $!;
82 15         142 print $wfh '1;';
83 15 50       603 close $wfh or die $!;
84             }
85             }
86             }
87             else {
88             # Single-word module, ie. no directory structure
89 2         9 $self->_create_lib_dir;
90              
91 2         8 my $module_file = "$module.pm";
92 2 50       42 if (! -f "$self->{lib}/$module_file") {
93             # Create the module file
94 2 50       122 open my $wfh, '>', "$self->{lib}/$module_file" or die $!;
95 2         24 print $wfh '1;';
96 2 50       72 close $wfh or die $!;
97             }
98             }
99             }
100              
101 6 50       38 if (! $self->{lib}) {
102 0         0 `perl -c $self->{file}`;
103             }
104             else {
105 6         165647 `perl -I$self->{lib} -c $self->{file}`;
106             }
107             }
108             sub _create_lib_dir {
109 17     17   48 my ($self) = @_;
110 17 100 66     262 if (! exists $self->{lib} || ! -d $self->{lib}) {
111 6 100       31 $self->{cleanup} = exists $self->{keep} ? ! $self->{keep} : 1;
112 6         53 $self->{lib} = tempdir(CLEANUP => $self->{cleanup});
113 6 100       2956 say "Created temp lib dir '$self->{lib}'" if $self->{verbose};
114             }
115             }
116       0     sub __placeholder {}
117              
118             1;
119             __END__