File Coverage

blib/lib/Test2/Util.pm
Criterion Covered Total %
statement 51 61 83.6
branch 12 32 37.5
condition 3 19 15.7
subroutine 14 14 100.0
pod 1 1 100.0
total 81 127 63.7


line stmt bran cond sub pod time code
1             package Test2::Util;
2 54     54   18743 use strict;
  54         59  
  54         1150  
3 54     54   137 use warnings;
  54         47  
  54         1676  
4              
5             our $VERSION = '0.000042';
6              
7 54     54   163 use Config qw/%Config/;
  54         161  
  54         2596  
8              
9             our @EXPORT_OK = qw{
10             try
11              
12             pkg_to_file
13              
14             get_tid USE_THREADS
15             CAN_THREAD
16             CAN_REALLY_FORK
17             CAN_FORK
18             };
19 54     54   221 use base 'Exporter';
  54         58  
  54         12807  
20              
21             sub _can_thread {
22 54 50   54   177 return 0 unless $] >= 5.008001;
23 54 50       376 return 0 unless $Config{'useithreads'};
24              
25             # Threads are broken on perl 5.10.0 built with gcc 4.8+
26 0 0 0     0 if ($] == 5.010000 && $Config{'ccname'} eq 'gcc' && $Config{'gccversion'}) {
      0        
27 0         0 my @parts = split /\./, $Config{'gccversion'};
28 0 0 0     0 return 0 if $parts[0] >= 4 && $parts[1] >= 8;
29             }
30              
31             # Change to a version check if this ever changes
32 0 0       0 return 0 if $INC{'Devel/Cover.pm'};
33 0         0 return 1;
34             }
35              
36             sub _can_fork {
37 54 50   54   6616 return 1 if $Config{d_fork};
38 0 0 0     0 return 0 unless $^O eq 'MSWin32' || $^O eq 'NetWare';
39 0 0       0 return 0 unless $Config{useithreads};
40 0 0       0 return 0 unless $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/;
41              
42 0         0 return _can_thread();
43             }
44              
45             BEGIN {
46 54     54   204 no warnings 'once';
  54         63  
  54         6212  
47 54 50   54   2714 *CAN_REALLY_FORK = $Config{d_fork} ? sub() { 1 } : sub() { 0 };
48 54 50       143 *CAN_THREAD = _can_thread() ? sub() { 1 } : sub() { 0 };
49 54 50       115 *CAN_FORK = _can_fork() ? sub() { 1 } : sub() { 0 };
50             }
51              
52             sub _manual_try(&;@) {
53 2     2   11 my $code = shift;
54 2         3 my $args = \@_;
55 2         1 my $err;
56              
57 2         7 my $die = delete $SIG{__DIE__};
58              
59 2 100 50     3 eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
  2         3  
  1         3  
60              
61 2 50       13 $die ? $SIG{__DIE__} = $die : delete $SIG{__DIE__};
62              
63 2         7 return (!defined($err), $err);
64             }
65              
66             sub _local_try(&;@) {
67 188     188   1009 my $code = shift;
68 188         247 my $args = \@_;
69 188         141 my $err;
70              
71 54     54   230 no warnings;
  54         63  
  54         5207  
72 188         585 local $SIG{__DIE__};
73 188 100 50     234 eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
  188         362  
  173         513  
74              
75 188         1363 return (!defined($err), $err);
76             }
77              
78             # Older versions of perl have a nasty bug on win32 when localizing a variable
79             # before forking or starting a new thread. So for those systems we use the
80             # non-local form. When possible though we use the faster 'local' form.
81             BEGIN {
82 54 50 33 54   282 if ($^O eq 'MSWin32' && $] < 5.020002) {
83 0         0 *try = \&_manual_try;
84             }
85             else {
86 54         6537 *try = \&_local_try;
87             }
88             }
89              
90             BEGIN {
91 54     54   77 if(CAN_THREAD) {
92             if ($INC{'threads.pm'}) {
93             # Threads are already loaded, so we do not need to check if they
94             # are loaded each time
95             *USE_THREADS = sub() { 1 };
96             *get_tid = sub { threads->tid() };
97             }
98             else {
99             # :-( Need to check each time to see if they have been loaded.
100             *USE_THREADS = sub { $INC{'threads.pm'} ? 1 : 0 };
101             *get_tid = sub { $INC{'threads.pm'} ? threads->tid() : 0 };
102             }
103             }
104             else {
105             # No threads, not now, not ever!
106 54         212 *USE_THREADS = sub() { 0 };
107 54         3652 *get_tid = sub() { 0 };
108             }
109             }
110              
111             sub pkg_to_file {
112 61     61 1 92 my $pkg = shift;
113 61         74 my $file = $pkg;
114 61         498 $file =~ s{(::|')}{/}g;
115 61         84 $file .= '.pm';
116 61         170 return $file;
117             }
118              
119             1;
120              
121             __END__