File Coverage

inc/Test2/Util.pm
Criterion Covered Total %
statement 32 72 44.4
branch 4 36 11.1
condition 0 16 0.0
subroutine 13 19 68.4
pod 3 3 100.0
total 52 146 35.6


line stmt bran cond sub pod time code
1             #line 1
2 6     6   40 package Test2::Util;
  6         14  
  6         168  
3 6     6   29 use strict;
  6         30  
  6         272  
4             use warnings;
5              
6             our $VERSION = '1.302073';
7              
8 6     6   64  
  6         12  
  6         452  
9             use Config qw/%Config/;
10              
11             our @EXPORT_OK = qw{
12             try
13              
14             pkg_to_file
15              
16             get_tid USE_THREADS
17             CAN_THREAD
18             CAN_REALLY_FORK
19             CAN_FORK
20              
21             IS_WIN32
22              
23             ipc_separator
24 6     6   36 };
  6         517  
25             BEGIN { require Exporter; our @ISA = qw(Exporter) }
26              
27 6 50   6   1546 BEGIN {
28             *IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 };
29             }
30              
31 6 50   6   35 sub _can_thread {
32 6 50       409 return 0 unless $] >= 5.008001;
33             return 0 unless $Config{'useithreads'};
34              
35 0 0 0       # Threads are broken on perl 5.10.0 built with gcc 4.8+
      0        
36 0           if ($] == 5.010000 && $Config{'ccname'} eq 'gcc' && $Config{'gccversion'}) {
37 0 0 0       my @parts = split /\./, $Config{'gccversion'};
      0        
38             return 0 if $parts[0] > 4 || ($parts[0] == 4 && $parts[1] >= 8);
39             }
40              
41 0 0         # Change to a version check if this ever changes
42 0           return 0 if $INC{'Devel/Cover.pm'};
43             return 1;
44             }
45              
46 0 0   0     sub _can_fork {
47 0 0         return 1 if $Config{d_fork};
48 0 0         return 0 unless IS_WIN32 || $^O eq 'NetWare';
49 0 0         return 0 unless $Config{useithreads};
50             return 0 unless $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/;
51 0            
52             return _can_thread();
53             }
54              
55 6     6   48 BEGIN {
  6         18  
  6         488  
56 6 50   6   27 no warnings 'once';
57             *CAN_THREAD = _can_thread() ? sub() { 1 } : sub() { 0 };
58             }
59             my $can_fork;
60 0 0   0 1   sub CAN_FORK () {
61             return $can_fork
62 0           if defined $can_fork;
63 6     6   40 $can_fork = !!_can_fork();
  6         21  
  6         767  
64 0 0         no warnings 'redefine';
65 0           *CAN_FORK = $can_fork ? sub() { 1 } : sub() { 0 };
66             $can_fork;
67             }
68             my $can_really_fork;
69 0 0   0 1   sub CAN_REALLY_FORK () {
70             return $can_really_fork
71 0           if defined $can_really_fork;
72 6     6   42 $can_really_fork = !!$Config{d_fork};
  6         11  
  6         1408  
73 0 0         no warnings 'redefine';
74 0           *CAN_REALLY_FORK = $can_really_fork ? sub() { 1 } : sub() { 0 };
75             $can_really_fork;
76             }
77              
78 0     0     sub _manual_try(&;@) {
79 0           my $code = shift;
80 0           my $args = \@_;
81             my $err;
82 0            
83             my $die = delete $SIG{__DIE__};
84 0 0 0        
  0            
  0            
85             eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
86 0 0          
87             $die ? $SIG{__DIE__} = $die : delete $SIG{__DIE__};
88 0            
89             return (!defined($err), $err);
90             }
91              
92 0     0     sub _local_try(&;@) {
93 0           my $code = shift;
94 0           my $args = \@_;
95             my $err;
96 6     6   42  
  6         13  
  6         853  
97 0           no warnings;
98 0 0 0       local $SIG{__DIE__};
  0            
  0            
99             eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
100 0            
101             return (!defined($err), $err);
102             }
103              
104             # Older versions of perl have a nasty bug on win32 when localizing a variable
105             # before forking or starting a new thread. So for those systems we use the
106             # non-local form. When possible though we use the faster 'local' form.
107 6     6   64 BEGIN {
108             if (IS_WIN32 && $] < 5.020002) {
109             *try = \&_manual_try;
110             }
111 6         1130 else {
112             *try = \&_local_try;
113             }
114             }
115              
116 6     6   57 BEGIN {
117             if (CAN_THREAD) {
118             if ($INC{'threads.pm'}) {
119             # Threads are already loaded, so we do not need to check if they
120             # are loaded each time
121             *USE_THREADS = sub() { 1 };
122             *get_tid = sub() { threads->tid() };
123             }
124             else {
125             # :-( Need to check each time to see if they have been loaded.
126             *USE_THREADS = sub() { $INC{'threads.pm'} ? 1 : 0 };
127             *get_tid = sub() { $INC{'threads.pm'} ? threads->tid() : 0 };
128             }
129             }
130             else {
131 6         19 # No threads, not now, not ever!
132 6         745 *USE_THREADS = sub() { 0 };
133             *get_tid = sub() { 0 };
134             }
135             }
136              
137 0     0 1   sub pkg_to_file {
138 0           my $pkg = shift;
139 0           my $file = $pkg;
140 0           $file =~ s{(::|')}{/}g;
141 0           $file .= '.pm';
142             return $file;
143             }
144              
145             sub ipc_separator() { "~" }
146              
147             1;
148              
149             __END__