File Coverage

blib/lib/Fukurama/Class/Rigid.pm
Criterion Covered Total %
statement 33 33 100.0
branch 10 12 83.3
condition 6 10 60.0
subroutine 6 6 100.0
pod 1 1 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             package Fukurama::Class::Rigid;
2             our $VERSION = 0.02;
3 14     14   50803 use strict;
  14         33  
  14         550  
4 14     14   81 use warnings;
  14         29  
  14         404  
5 14     14   7750 use Fukurama::Class::Carp;
  14         36  
  14         151  
6              
7             our $PACKAGE_NAME_CHECK = 1;
8             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 and B 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 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 This method can only be called inside of an B 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
62              
63             =cut
64              
65              
66             # AUTOMAGIC void
67             sub import {
68 78     78   6994 my $class = $_[0];
69 78   50     477 my $import_depth = $_[1] || 0;
70            
71 78         252 $class->rigid($import_depth + 1);
72 76         5188 return undef;
73             }
74             # boolean
75             sub rigid {
76 102     102 1 209 my $class = $_[0];
77 102   50     274 my $import_depth = $_[1] || 0;
78            
79 102         1315 strict::import();
80 102 50       1070 warnings::import() if(!$DISABLE);
81 102 100       258 if($PACKAGE_NAME_CHECK) {
82 78         475 my $caller = [caller($import_depth)];
83 78 50 66     705 if($caller->[0] ne 'main' && $caller->[0] ne '__ANON__' && $caller->[1] !~ m/^\(eval.+\)$/) {
      66        
84 75         279 my $filename = $class->_guess_packagename($caller->[1]);
85 75         332 $filename =~ s/\.[a-z]*$//i;
86 75         139 $filename =~ s/^\.+\/+//;
87              
88 75         424 my @path = split(/[\/\\]/, $filename);
89 75         283 my $should = join('::', splice(@path, 0, scalar(@path)));
90 75 100       459 if($should ne $caller->[0]) {
91 2 100       18 _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);
92             }
93             }
94             }
95 100         190 return 1;
96             }
97             # string
98             sub _guess_packagename {
99 75     75   576 my $class = $_[0];
100 75         96 my $filename = $_[1];
101            
102 75         95 do {
103 510 100       3246 return $filename if($INC{$filename});
104             } while($filename =~ s/^[^\/]*\///);
105 1         3 return '';
106             }
107             1;