Class/Rigid.pm

filter: Statementstmt 100% 37 / 37 Branchbran 92.8% 13 / 14 Conditioncond 60.0% 6 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 87.6% 64 / 73 CC 12 SCAR 17.6
CC
12
Cov
92%
rigid
9.1
_guess_packagename
3.0
import
2.0
CRAP 5.8
« Class/DataTypes.pm ↑ Summary Class/Extends.pm »
1 package Fukurama::Class::Rigid;
2 13 our $VERSION = 0.02;
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Fukurama::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $PACKAGE_NAME_CHECK = 1;
8 13 our $DISABLE = 0;
9  
10 =head1 NAME
11  
12 Fukurama::Class::Rigid - Pragma to set strict and warnings pragma and check classnames
13  
14 =head1 VERSION
15  
16 Version 0.02 (beta)
17  
18 =head1 SYNOPSIS
19  
20 package MyClass;
21 use Fukurama::Class::Rigid;
22  
23 =head1 DESCRIPTION
24  
25 This pragma-like module provides set the B<strict> and B<warnings> pragma in the caller module. It will also
26 check the class- and filename of the package and croak at compiletime, if they are inconsistent.
27  
28 =head1 CONFIG
29  
30 You can disable the class- and filename check by setting. You have to do this at compiletime BEFORE any
31 B<use Fukurama::Class::Rigid;> is executed.
32  
33 $Fukurama::Class::Rigid::PACKAGE_NAHE_CHECK = 0;
34  
35 You even can disable warnings by saying:
36  
37 $Fukurama::Class::Rigid::DISABLE = 1;
38  
39 to speed up your code (Warnings are even executed at runtime).
40  
41 =head1 EXPORT
42  
43 nothing, bit the behavior of the strict and warnings pragmas.
44  
45 =head1 METHODS
46  
47 =over 4
48  
49 =item rigid( import_depth:INT ) return:VOID
50  
51 export warning() and strict() behavior to the caller and check the package name of callers class. With the
52 import_depht parameter you can define for which caller, the first, second etc, this behavior should be exported.
53  
54 B<ATTENTION!> This method can only be called inside of an B<import()> method at compiletime. Otherwise warnings() and
55 strict() would not work.
56  
57 =back
58  
59 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
60  
61 see perldoc of L<Fukurama::Class>
62  
63 =cut
64  
65  
66 # AUTOMAGIC void
67 64 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
68 64 my $class = $_[0];
69 64 ▶ my $import_depth = $_[1] || 0;
Condition50%1/2
$_[1] || 0
l!l
064
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
70
71 64 $class->rigid($import_depth + 1);
72 62 return undef;
73 }
74 # boolean
75 65 ▶sub rigid {
Subroutine rigid: called CC=9 SCAR=22.1
Pod documented
76 65 my $class = $_[0];
77 65 ▶ my $import_depth = $_[1] || 0;
Condition50%1/2
$_[1] || 0
l!l
650
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
78
79 65 strict::import();
80 65 ▶ warnings::import() if(!$DISABLE);
Branch: 1 branch
BranchTrueFalseTotal
unless $Fukurama::Class::Rigid::DISABLE 65 0 65
81 65 ▶ if($PACKAGE_NAME_CHECK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Fukurama::Class::Rigid::PACKAGE_NAME_CHECK) 64 1 65
82 64 my $caller = [caller($import_depth)];
83 64 ▶ if($caller->[0] ne 'main' && $caller->[0] ne '__ANON__' && $caller->[1] !~ m/^\(eval.+\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller->[0] ne "main" and $caller->[0] ne "__ANON__" and not $caller->[1] =~ /^\(eval.+\)$/) 61 3 64
Condition66%4/6
$caller->[0] ne "main" and $caller->[0] ne "__ANON__" and not $caller->[1] =~ /^\(eval.+\)$/
!ll&&!rl&&r
3061
$caller->[0] ne "main" and $caller->[0] ne "__ANON__"
!ll&&!rl&&r
3061
MC/DC33%1/3
$caller->[0] ne "main" and $caller->[0] ne "__ANON__" and not $caller->[1] =~ /^\(eval.+\)$/
$caller->[0] ne "main" $caller->[0] ne "__ANON__" not $caller->[1] =~ /^\(eval.+\)$/
Truth table50%2/4
A and B and not(C)
A= $caller->[0] ne "main"B= $caller->[0] ne "__ANON__"C= not $caller->[1] =~ /^\(eval.+\)$/
ABCresult
0XX0
10X0
1100
1111
84 61 my $filename = $class->_guess_packagename($caller->[1]);
85 61 $filename =~ s/\.[a-z]*$//i;
86 61 $filename =~ s/^\.+\/+//;
87  
88 61 my @path = split(/[\/\\]/, $filename);
89 61 my $should = join('::', splice(@path, 0, scalar(@path)));
90 61 ▶ if($should ne $caller->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should ne $caller->[0]) 2 59 61
91 2 ▶ _croak("Wrong package name '$caller->[0]'. " . ($should ? "You should use '$should'" : "Can't guess correct package name. Maybe an inline-class or a test?."), $import_depth);
Branch: 1 branch
BranchTrueFalseTotal
$should ? : 1 1 2
92 }
93 }
94 }
95 63 return 1;
96 }
97 # string
98 61 ▶sub _guess_packagename {
Subroutine _guess_packagename: called CC=3 SCAR=11.0
99 61 my $class = $_[0];
100 61 my $filename = $_[1];
101
102 61 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ s[^[^/]*/][] and do { do { return $filename if $INC{$filename} } } 467 1 468
103 528 ▶ return $filename if($INC{$filename});
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$filename} 60 468 528
104 } while($filename =~ s/^[^\/]*\///);
105 1 return '';
106 }
107 13 1;
« Class/DataTypes.pm ↑ Summary Class/Extends.pm »