blib/lib/Eobj.pm

filter: Statementstmt 51.0% 126 / 247 Branchbran 19.7% 17 / 86 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 65.6% 21 / 32 Podpod 0.0% 0 / 10 totaltotal 43.1% 164 / 380 CC 47 SCAR 34.8
CC
47
Cov
43%
do_inheritdir
132.0
linebreak
72.0
override
30.0
CRAP 32.6
« blib/lib/Eobj/PLroot.pl ↑ Summary blib/lib/Eobj/site_init.pl »
1 #
2 # This file is part of the Eobj project.
3 #
4 # Copyright (C) 2003, Eli Billauer
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 # A copy of the license can be found in a file named "licence.txt", at the
21 # root directory of this project.
22 #
23  
24 1 ▶use Eobj::PLerror;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 package Eobj;
26  
27 1 ▶use Eobj::PLerror;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 require Exporter;
34  
35  
36 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.6
37 1 @Eobj::warnings = ();
38 1 %Eobj::classes = ();
39 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 0 my ($class) = ($_[0] =~ /unquoted string.*?\"(.*?)\".*may clash/i);
41 0 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) { } 0 0 0
42 0 push @Eobj::warnings, $_[0];
43 } else {
44 0 warn ($_[0])
45 }
46 1 };
47 }
48  
49 1 ▶END {
Subroutine END: called CC=4 SCAR=17.1
50 0 ▶ $SIG{__WARN__} = sub {warn $_[0]; }; # Prevent an endless recursion
Subroutine __ANON__: not called CC=1 SCAR=1.2
51 1 foreach (@Eobj::warnings) {
52 0 my ($class) = ($_ =~ /unquoted string.*?\"(.*?)\".*may clash/i);
53 warn ($_)
54 0 ▶ unless (defined $Eobj::classes{$class});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Eobj::classes{$class} 0 0 0
55 }
56  
57 # Now we destroy all objects in an orderly fashion...
58 1 foreach (sort { $b->get('eobj-object-count') <=> $a->get('eobj-object-count') }
59 values %Eobj::objects) {
60 2 $_->survivor();
61 2 $_->destroy();
62 }
63 }
64  
65 # We use explicit package names rather than Perl 5.6.0's "our", so
66 # perl 5.004 won't yell at us.
67  
68 1 @Eobj::ISA = qw[Exporter];
69 1 @Eobj::EXPORT = qw[&init &override &underride &inherit &inheritdir &definedclass &globalobj];
70 1 $Eobj::VERSION = '0.23';
71 1 $Eobj::STARTTIME = localtime();
72  
73 1 $Eobj::eobjflag = 0;
74 1 $Eobj::globalobject=();
75  
76 1 ▶unless ($Eobj::eobjflag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Eobj::eobjflag) 1 0 1
77 1 $Eobj::eobjflag = 1; # Indicate that this clause has been run once
78 1 $Eobj::errorcrawl='system';
79 1 $Eobj::callbacksdepth = 0; # This indicates when callbacks are going on.
80 1 undef $Eobj::wrong_flag;
81  
82 #For unloaded classes: Value is [classfile, parent class, first-given classname].
83 1 %Eobj::classes = ('PL_hardroot', 1);
84 1 %Eobj::objects = ();
85 1 $Eobj::objectcounter = 0;
86
87 {
88 1 my $home = $INC{'Eobj.pm'};
89 1 ($home) = ($home =~ /^(.*)Eobj\.pm$/);
90 1 ▶ blow("Failed to resolve Eobj.pm's directory")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $home 0 1 1
91 unless (defined $home);
92 1 $Eobj::home = $home;
93 }
94  
95 1 $Eobj::classhome = "${Eobj::home}Eobj/";
96 1 inherit('root',"${Eobj::classhome}PLroot.pl",'PL_hardroot');
97 1 inherit('global',"${Eobj::classhome}PLglobal.pl",'root');
98 1 inherit('site_init',"${Eobj::classhome}site_init.pl",'PL_hardroot');
99 }
100  
101 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
102 1 site_init -> init;
103 }
104 4 ▶sub inherit {
Subroutine inherit: called CC=3 SCAR=11.1
Pod undocumented
105 4 my $class = shift;
106 4 my $file = shift;
107 4 my $papa = shift;
108  
109 puke("Attempt to create the already existing class \'$class\'\n")
110 4 ▶ if $Eobj::classes{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $Eobj::classes{$class} 0 4 4
111  
112 4 ▶ puke("No parent class defined for \'$class\'\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $papa 0 4 4
113 unless (defined $papa);
114 4 $Eobj::classes{$class} = [$file, $papa, $class];
115 # The following two lines are a Perl 5.8.0 bug workaround (early
116 # versions). Google "stash autoload" for why.
117 4 undef ${"${class}::Eobj_dummy_variable"};
118 4 undef ${"${class}::Eobj_dummy_variable"}; # No single use warning...
119 4 return 1;
120 }
121  
122 0 ▶sub inheritdir {
Subroutine inheritdir: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my $dir = shift;
124 0 my $papa = shift;
125  
126 0 ($dir) = ($dir =~ /^(.*?)[\/\\]*$/); # Remove trailing slashes
127  
128 0 ▶ blow("Nonexistent directory \'$dir\'\n")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
129 unless (-d $dir);
130  
131 0 do_inheritdir($dir, $papa);
132 0 return 1;
133 }
134  
135 0 ▶sub do_inheritdir {
Subroutine do_inheritdir: not called CC=11 SCAR=48.8
Pod undocumented
136 0 my $dir = shift;
137 0 my $papa = shift;
138  
139 0 ($dir) = ($dir =~ /^(.*?)[\/\\]*$/); # Remove trailing slashes
140  
141 0 ▶ return unless (opendir(DIR,$dir));
Branch: 1 branch
BranchTrueFalseTotal
unless opendir DIR, $dir 0 0 0
142 0 my @files=sort readdir(DIR);
143 0 closedir(DIR);
144 0 my @dirs = ();
145 0 my %newclasses = ();
146  
147 0 foreach my $file (@files) {
148 0 ▶ next if (($file eq '.') || ($file eq '..'));
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "." or $file eq ".." 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
149 0 my $thefile = $dir.'/'.$file;
150  
151 0 ▶ if (-d $thefile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $thefile) { } 0 0 0
152 0 ▶ next unless ($file =~ /^[a-zA-Z][a-zA-Z0-9_]*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^[a-zA-Z][a-zA-Z0-9_]*$/ 0 0 0
153 0 push @dirs, $file, $thefile;
154 } else {
155 0 my ($class) = ($file =~ /^([a-zA-Z][a-zA-Z0-9_]*)\.pl$/i);
156 0 ▶ next unless (defined $class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
157 0 $class = lc $class; # Lowercase the class
158 blow("inheritdir: Attempt to create the already existing class \'".$class.
159 "\' with \'$thefile\' (possibly symbolic link loop?)\n")
160 0 ▶ if ($Eobj::classes{$class});
Branch: 1 branch
BranchTrueFalseTotal
if $Eobj::classes{$class} 0 0 0
161 0 inherit($class, $thefile, $papa);
162 0 $newclasses{$class} = 1;
163 }
164 }
165 0 ▶ while ($#dirs > 0) { # At least two entries...
Branch: 1 branch
BranchTrueFalseTotal
if ($#dirs > 0) 0 0 0
166 0 my $newpapa = lc shift @dirs;
167 0 my $descend = shift @dirs;
168
169 blow("inheritdir: Could not descend to directory \'$descend\' because there was no \'".
170 $newpapa.".pl\' file in directory \'$dir\'\n")
171 0 ▶ unless ($newclasses{$newpapa});
Branch: 1 branch
BranchTrueFalseTotal
unless $newclasses{$newpapa} 0 0 0
172 0 do_inheritdir($descend, $newpapa);
173 }
174 }
175  
176 0 ▶sub override {
Subroutine override: not called CC=5 SCAR=34.0
Pod undocumented
177 0 my $class = shift;
178 0 my $file = shift;
179 0 my $papa = shift;
180  
181 0 ▶ unless ($Eobj::classes{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Eobj::classes{$class}) 0 0 0
182 0 ▶ return inherit($class, $file, $papa)
Branch: 1 branch
BranchTrueFalseTotal
if defined $papa 0 0 0
183 if defined ($papa);
184 0 puke("Tried to override nonexisting class \'$class\', and no alternative parent given\n");
185 }
186  
187 puke("Attempt to override class \'$class\' after it has been loaded\n")
188 0 ▶ unless ref($Eobj::classes{$class});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Eobj::classes{$class} 0 0 0
189  
190 # Now create a new name for the previous class pointer
191  
192 0 my $newname=$class.'_PL_';
193 0 my $i=1;
194 0 ▶ while (defined $Eobj::classes{$newname.$i}) {$i++;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Eobj::classes{$newname . $i}) 0 0 0
195 0 $newname=$newname.$i;
196
197 # This is the operation of overriding
198  
199 0 $Eobj::classes{$newname}=$Eobj::classes{$class};
200 0 $Eobj::classes{$class}=[$file, $newname, $class];
201  
202 # The following two lines are a Perl 5.8.0 bug workaround (early
203 # versions). Google "stash autoload" for why.
204 0 undef ${"${newname}::Eobj_dummy_variable"};
205 0 undef ${"${newname}::Eobj_dummy_variable"}; # No single use warning
206  
207 0 return 1;
208 }
209  
210 0 ▶sub underride {
Subroutine underride: not called CC=5 SCAR=34.0
Pod undocumented
211 0 my $class = shift;
212 0 my $file = shift;
213  
214 0 ▶ unless ($Eobj::classes{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Eobj::classes{$class}) 0 0 0
215 0 puke("Tried to underride a nonexisting class \'$class\'\n");
216 }
217  
218 puke("Attempt to underride class \'$class\' after it has been loaded\n")
219 0 ▶ unless ref($Eobj::classes{$class});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Eobj::classes{$class} 0 0 0
220  
221 # Now create a new name for the previous class pointer
222  
223 0 my $newname=$class.'_PL_';
224 0 my $i=1;
225 0 ▶ while (defined $Eobj::classes{$newname.$i}) {$i++;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Eobj::classes{$newname . $i}) 0 0 0
226 0 $newname=$newname.$i;
227
228 0 my $victim = $class;
229  
230 # Now we look for the grandfather
231 0 SEARCH: while (1) {
232 0 my $parent = ${$Eobj::classes{$victim}}[1];
233 0 ▶ if (${$Eobj::classes{$parent}}[2] ne $class) { # Same family?
Branch: 1 branch
BranchTrueFalseTotal
if (do { $Eobj::classes{$parent} }->[2] ne $class) { } 0 0 0
234 0 last SEARCH;
235 } else {
236 0 $victim = $parent; # Climb up the family tree
237 }
238 }
239 # This is the operation of parenting
240  
241 0 $Eobj::classes{$newname}=[$file, ${$Eobj::classes{$victim}}[1], $class];
242 0 ${$Eobj::classes{$victim}}[1]=$newname;
243  
244 # The following two lines are a Perl 5.8.0 bug workaround (early
245 # versions). Google "stash autoload" for why.
246 0 undef ${"${newname}::Eobj_dummy_variable"};
247 0 undef ${"${newname}::Eobj_dummy_variable"}; # No single use warning.
248 0 return 1;
249 }
250  
251 #definedclass:
252 #0 - not defined, 1 - defined but not loaded, 2 - defined and loaded
253  
254 0 ▶sub definedclass {
Subroutine definedclass: not called CC=3 SCAR=24.8
Pod undocumented
255 0 my $class = shift;
256 0 my $p = $Eobj::classes{$class};
257 0 ▶ return 0 unless (defined $p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 0 0
258 0 ▶ return 1 if ref($p);
Branch: 1 branch
BranchTrueFalseTotal
if ref $p 0 0 0
259 0 return 2;
260 }
261  
262 7 ▶sub classload {
Subroutine classload: called CC=5 SCAR=16.1
Pod undocumented
263 7 my ($class, $schwonz) = @_;
264 7 my $p = $Eobj::classes{$class};
265 7 my $err;
266  
267 7 ▶ blow($schwonz."Attempt to use undeclared class \'$class\'\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 7 7
268 unless (defined $p);
269  
270 # If $p isn't a reference, the class has been loaded.
271 # This trick allows recursive calls.
272 7 ▶ return 1 unless ref($p);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p 3 4 7
273  
274 4 $Eobj::classes{$class} = 1;
275  
276 4 my ($file, $papa, $original) = @{$p};
277  
278 4 classload($papa, $schwonz); # Make sure parents are loaded
279  
280 # Now we create the package wrapping
281  
282 4 my $d = "package $class; use strict 'vars'; use Eobj::PLerror;\n";
283 4 $d.='@'.$class."::ISA=qw[$papa];\n";
284  
285 # Registering MUST be the last line before the text itself,
286 # since the line number is recorded. Line count in error
287 # messages begin immediately after the line that registers.
288  
289 4 $d.="&Eobj::PLerror::register(\'$file\');\n# line 1 \"$file\"\n";
290  
291 4 ▶ open (CLASSFILE, $file) ||
Branch: 1 branch
BranchTrueFalseTotal
open CLASSFILE, $file or blow($schwonz . "Failed to open resource file '${file}' for class '${class}'\n") 4 0 4
292 blow($schwonz."Failed to open resource file \'$file\' for class \'$class\'\n");
293 4 $d.=join("",<CLASSFILE>);
294 4 close CLASSFILE;
295 1 ▶ eval($d);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
296 4 ▶ blow ($schwonz."Failed to load class \'$original\':\n $@")
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
297 if ($@);
298 }
299  
300 0 ▶sub globalobj {
Subroutine globalobj: not called CC=2 SCAR=17.9
Pod undocumented
301 0 ▶ return $Eobj::globalobject if (ref $Eobj::globalobject);
Branch: 1 branch
BranchTrueFalseTotal
if ref $Eobj::globalobject 0 0 0
302 0 puke("Global object was requested before init() was executed\n");
303 }
304  
305 # This routine attempts to keep lines below 80 chrs/lines
306 0 ▶sub linebreak {
Subroutine linebreak: not called CC=8 SCAR=42.8
Pod undocumented
307 0 my $data = shift;
308 0 my $extraindent = shift;
309  
310 0 ▶ $extraindent = '' unless (defined $extraindent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extraindent 0 0 0
311  
312 0 my @chunks = split("\n", $data);
313  
314 0 foreach (@chunks) {
315 0 my $realout = '';
316 0 while (1) { # Not forever. We'll break this in proper time
317 0 ▶ if (/^.{0,79}$/) { # The rest fits well...
Branch: 1 branch
BranchTrueFalseTotal
if (/^.{0,79}$/) 0 0 0
318 0 $realout .= $_;
319 0 last;
320 }
321 # We try to break the line after a comma.
322 0 my ($x, $y) = (/^(.{50,78},)\s*(.*)$/);
323 # Didn't work? A whitespace is enough, then.
324 0 ▶ ($x, $y) = (/^(.{50,79})\s+(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 0 0
325 unless (defined $x);
326 # Still didn't work? Break at first white space.
327 0 ▶ ($x, $y) = (/^(.{50,}?)\s+(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 0 0
328 unless (defined $x);
329
330 # THAT didn't work? Give up. Just dump it all out.
331 0 ▶ unless (defined $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) { } 0 0 0
332 0 $realout .= $_;
333 0 last;
334 } else { # OK, we have a line split!
335 0 $realout .= $x."\n";
336 0 $_ = $extraindent.$y; # The rest, only indented.
337 }
338 }
339 0 $_ = $realout;
340 }
341 0 my $final = join("\n", @chunks);
342 0 ▶ $final .= "\n" if ($data =~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /\n$/ 0 0 0
343 0 return $final;
344 }
345  
346 # Just empty packages (used by PLroot).
347 package PL_hardroot;
348 package PL_settable;
349 package PL_const;
350 package PL_destroyed;
351 1 ▶use Eobj::PLerror;
Subroutine BEGIN: called CC=1 SCAR=0.0
352 # Here we yell on all attempts to run a method on a destroyed
353 # object. Only calling destroy is OK...
354 1 $PL_destroyed::errorcrawl='system';
355  
356 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
357 0 return undef;
358 }
359  
360 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
361 0 my $class = shift;
362 0 my $method = $PL_destroyed::AUTOLOAD;
363 0 my ($package) = $method =~ /^(.*?)::/;
364 0 $method =~ s/.*:://;
365  
366 0 ▶ return undef if ($method eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
367  
368 0 blow("Attempt to call method \'$method\' on a destroyed object\n");
369 }
370  
371 # And now the magic of autoloading.
372 package UNIVERSAL;
373 1 ▶use Eobj::PLerror;
Subroutine BEGIN: called CC=1 SCAR=0.0
374 1 $UNIVERSAL::errorcrawl='skip';
375 1 %UNIVERSAL::blacklist=();
376  
377 74813 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=7 SCAR=19.8
378 74813 my $class = shift;
379 74813 my $method = $UNIVERSAL::AUTOLOAD;
380 74813 my ($junk,$file,$line)=caller;
381 74813 my $schwonz = "at $file line $line";
382 74813 ▶ return undef if $method =~ /::SUPER::/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /::SUPER::/ 2 74811 74813
383  
384 74811 my ($package) = $method =~ /^(.*?)::/;
385 74811 $method =~ s/.*:://;
386  
387 74811 my $name = ref($class);
388  
389 74811 ▶ return undef if ($method eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 74808 3 74811
390
391 3 ▶ print "$class, $package\n" unless ($class eq $package);
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq $package 0 3 3
392 3 ▶ puke("Undefined function/method \'$method\' $schwonz\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq $package 0 3 3
393 unless ($class eq $package);
394  
395 3 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 3 3
396 # Forgive. This is not our class anyway...
397 0 return undef;
398 }
399  
400 # Now we protect ourselves against infinite recursion, should
401 # the classload call fail silently. This will happen if the
402 # first attempt to call a method in a class is to a
403 # method that isn't defined.
404 puke("Undefined method \'$method\' in class \'$class\' $schwonz\n")
405 3 ▶ if $UNIVERSAL::blacklist{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $UNIVERSAL::blacklist{$class} 0 3 3
406 3 $UNIVERSAL::blacklist{$class}=1;
407  
408 3 &Eobj::classload($class,
409 "While trying to load class \'$class\' due to call ".
410 "of method \'$method\' $schwonz:\n");
411
412 #Just loaded the new class? Let's use it!
413 3 return $class->$method(@_);
414 }
415  
416 # Now have the "defineclass" subroutine defined, so we can use it to
417 # generate bareword warnings for anything but a class name.
418  
419  
420  
421 1 1; # Return true
422 __END__
« blib/lib/Eobj/PLroot.pl ↑ Summary blib/lib/Eobj/site_init.pl »