| 1 | |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | require 5; | |||||||||||||||||||||||||||||||||||||||||
| 3 | package strict::ModuleName; # Pod at end | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | BEGIN { *DEBUG = sub () {0} unless defined &DEBUG } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use vars qw($DIE); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | $DIE = 1 unless defined $DIE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=21 SCAR=39.2
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | # Make sure that the calling package's name agrees with its filename | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 13 | ▶ | if(@_ > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | for my $msg ( | |||||||||||||||||||||||||||||||||||||||||
| 15 | "Proper usage: use " . __PACKAGE__ . "; #(with no parameters) " | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | ) { $DIE ? Carp::croak($msg) : Carp::carp($msg) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 13 | my($package, $filename) = caller(0); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | ▶ | unless($filename =~ m/.\.pm$/s) { # catch this first off | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if($filename =~ m/.(\.pm)$/is) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | for my $msg ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | "filename \"$filename\" should end in \".pm\", not \"$1\"\n" | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | ) { return $DIE ? die($msg) : warn($msg) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | for my $msg ( | |||||||||||||||||||||||||||||||||||||||||
| 27 | "filename \"$filename\" should end in \".pm\"!\n" | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | ) { return $DIE ? die($msg) : warn($msg) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 13 | my $pre = quotemeta($package); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 13 | $pre =~ s/(\\[\'\:])+/./g; # Foo::Bar => Foo.Bar | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 13 | DEBUG and print ">>>>. $package in $filename\n"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | my $re = join '', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | '^(', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | join('|', map quotemeta($_), | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 13 | 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 | 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 | ▶ | if($filename =~ m/$re/s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | 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 | my @callstack; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | my $back_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | my $real_depth = 0; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | while(1) { | |||||||||||||||||||||||||||||||||||||||||
| 71 | 72 | ▶ | last unless defined caller($back_count); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 63 | my $sub_name = (caller($back_count))[3]; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 63 | ▶ | ++$real_depth | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sub_name eq "(eval)" or $sub_name =~ /\:\:BEGIN$/s
MC/DC100%2/2
$sub_name eq "(eval)" or $sub_name =~ /\:\:BEGIN$/s
$sub_name eq "(eval)"
$sub_name =~ /\:\:BEGIN$/s
Truth table100%3/3
A or B
A= $sub_name eq "(eval)"B= $sub_name =~ /\:\:BEGIN$/s
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | unless $sub_name eq '(eval)' or $sub_name =~ m/\:\:BEGIN$/s; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 63 | ++$back_count; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 9 | my $fn = $filename; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 9 | ▶ | $fn =~ s/\.pm$//s or die "WHAAAAAT?"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | if($real_depth == 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$real_depth == 1 and length $fn <= length $package and substr($package, 0 - length($fn)) eq $fn
$real_depth == 1 and length $fn <= length $package
MC/DC0%0/3
$real_depth == 1 and length $fn <= length $package and substr($package, 0 - length($fn)) eq $fn
$real_depth == 1
length $fn <= length $package
substr($package, 0 - length($fn)) eq $fn
Truth table25%1/4
A and B and C
A= $real_depth == 1B= length $fn <= length $packageC= substr($package, 0 - length($fn)) eq $fn
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | and length($fn) <= length($package) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | and substr($package, 0 - length($fn)) eq $fn | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 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 | return; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | if(grep ref($_), @INC) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 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 | return; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 9 | for my $msg ( | |||||||||||||||||||||||||||||||||||||||||
| 100 | "file \"$filename\" producing package \"$package\" is bad\n" | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | ▶ | ) { return $DIE ? die($msg) : warn($msg) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | &import(); # Yes, test myself! | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||||||||||||