File Coverage

blib/lib/strict/ModuleName.pm
Criterion Covered Total %
statement 34 46 73.9
branch 13 28 46.4
condition 5 9 55.5
subroutine 4 4 100.0
pod n/a
total 56 87 64.3


line stmt bran cond sub pod time code
1              
2             require 5;
3             package strict::ModuleName; # Pod at end
4             $VERSION = '0.04';
5 1     1   846 use strict;
  1         2  
  1         75  
6 1 50   1   26 BEGIN { *DEBUG = sub () {0} unless defined &DEBUG }
7 1     1   6 use vars qw($DIE);
  1         2  
  1         852  
8             $DIE = 1 unless defined $DIE;
9              
10             sub import {
11             # Make sure that the calling package's name agrees with its filename
12 13 50   13   7425 if(@_ > 1) {
13 0         0 require Carp;
14 0         0 for my $msg (
15             "Proper usage: use " . __PACKAGE__ . "; #(with no parameters) "
16 0 0       0 ) { $DIE ? Carp::croak($msg) : Carp::carp($msg) }
17             }
18 13         53 my($package, $filename) = caller(0);
19              
20 13 50       66 unless($filename =~ m/.\.pm$/s) { # catch this first off
21 0 0       0 if($filename =~ m/.(\.pm)$/is) {
22 0         0 for my $msg (
23             "filename \"$filename\" should end in \".pm\", not \"$1\"\n"
24 0 0       0 ) { return $DIE ? die($msg) : warn($msg) }
25             } else {
26 0         0 for my $msg (
27             "filename \"$filename\" should end in \".pm\"!\n"
28 0 0       0 ) { return $DIE ? die($msg) : warn($msg) }
29             }
30             }
31              
32 13         28 my $pre = quotemeta($package);
33 13         47 $pre =~ s/(\\[\'\:])+/./g; # Foo::Bar => Foo.Bar
34            
35 13         14 DEBUG and print ">>>>. $package in $filename\n";
36              
37 403         616 my $re = join '',
38             '^(',
39             join('|', map quotemeta($_),
40 13         43 sort {length($b) <=> length($a)}
41             @INC
42             ),
43             ')',
44             '\W{0,2}',
45             # generous RE matching trailing pathsep thing like / or \ or :
46             $pre,
47             '\.pm$',
48             ;
49              
50 13         32 if(DEBUG) {
51             DEBUG and print $re, "\n\n";
52             for(0 .. 10) {
53             print("\n"), last unless defined caller($_);
54             print "caller($_) is ", join(" # ", map $_ || '', (caller($_))[0..7] ), "\n";
55             }
56             }
57            
58 13 100       1760 if($filename =~ m/$re/s) {
59 4         7 DEBUG and print "file \"$filename\" producing package \"$package\" is okay\n";
60            
61             } else {
62            
63             {
64             # Jump thru hoops to check for a very common case:
65             # whether that package was like "perl -cw X.pm" or "perl -w X.pm"
66            
67 9         10 my @callstack;
  9         14  
68 9         14 my $back_count = 0;
69 9         10 my $real_depth = 0;
70 9         10 while(1) {
71 72 100       155 last unless defined caller($back_count);
72 63         175 my $sub_name = (caller($back_count))[3];
73 63 100 100     238 ++$real_depth
74             unless $sub_name eq '(eval)' or $sub_name =~ m/\:\:BEGIN$/s;
75 63         80 ++$back_count;
76             }
77 9         12 my $fn = $filename;
78 9 50       44 $fn =~ s/\.pm$//s or die "WHAAAAAT?";
79            
80 9 50 33     55 if($real_depth == 1
      33        
81             and length($fn) <= length($package)
82             and substr($package, 0 - length($fn)) eq $fn
83             ) {
84 0         0 warn( # yes, merely warn
85             "Can't verify whether package name \"$package\" is good in \"$filename\""
86             . "\n -- Instead try: perl -M$package -e -1\n"
87             );
88 0         0 return;
89             }
90             }
91            
92 9 50       38 if(grep ref($_), @INC) {
93 0         0 warn(
94             "file \"$filename\" producing package \"$package\" may be bad,\n"
95             . " -- but I can't be sure, because there's coderefs in \@INC\n");
96 0         0 return;
97             }
98            
99 9         28 for my $msg (
100             "file \"$filename\" producing package \"$package\" is bad\n"
101 9 50       339 ) { return $DIE ? die($msg) : warn($msg) }
102             }
103            
104 4         96 return;
105             }
106              
107             &import(); # Yes, test myself!
108              
109             1;
110              
111             __END__