File Coverage

blib/lib/ExtUtils/Constant/Utils.pm
Criterion Covered Total %
statement 53 55 96.3
branch 3 6 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 67 74 90.5


line stmt bran cond sub pod time code
1             package ExtUtils::Constant::Utils;
2              
3 1     1   8 use strict;
  1         3  
  1         43  
4 1     1   8 use vars qw($VERSION @EXPORT_OK @ISA);
  1         3  
  1         70  
5 1     1   6 use Carp;
  1         2  
  1         80  
6              
7             @ISA = 'Exporter';
8             @EXPORT_OK = qw(C_stringify perl_stringify);
9             $VERSION = '0.04';
10              
11 1     1   6 use constant is_perl55 => ($] < 5.005_50);
  1         1  
  1         98  
12 1   33 1   8 use constant is_perl56 => ($] < 5.007 && $] > 5.005_50);
  1         4  
  1         71  
13 1     1   8 use constant is_sane_perl => $] > 5.007;
  1         3  
  1         873  
14              
15             =head1 NAME
16              
17             ExtUtils::Constant::Utils - helper functions for ExtUtils::Constant
18              
19             =head1 SYNOPSIS
20              
21             use ExtUtils::Constant::Utils qw (C_stringify);
22             $C_code = C_stringify $stuff;
23              
24             =head1 DESCRIPTION
25              
26             ExtUtils::Constant::Utils packages up utility subroutines used by
27             ExtUtils::Constant, ExtUtils::Constant::Base and derived classes. All its
28             functions are explicitly exportable.
29              
30             =head1 USAGE
31              
32             =over 4
33              
34             =item C_stringify NAME
35              
36             A function which returns a 7 bit ASCII correctly \ escaped version of the
37             string passed suitable for C's "" or ''. It will die if passed Unicode
38             characters.
39              
40             =cut
41              
42             # Hopefully make a happy C identifier.
43             sub C_stringify {
44 209     209 1 1026 local $_ = shift;
45 209 50       466 return unless defined $_;
46             # grr 5.6.1
47 209 50       524 confess "Wide character in '$_' intended as a C identifier"
48             if tr/\0-\377// != length;
49             # grr 5.6.1 more so because its regexps will break on data that happens to
50             # be utf8, which includes my 8 bit test cases.
51 209         357 $_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if is_perl56;
52 209         515 s/\\/\\\\/g;
53 209         495 s/([\"\'])/\\$1/g; # Grr. fix perl mode.
54 209         421 s/\n/\\n/g; # Ensure newlines don't end up in octal
55 209         345 s/\r/\\r/g;
56 209         523 s/\t/\\t/g;
57 209         346 s/\f/\\f/g;
58 209         370 s/\a/\\a/g;
59 209         314 unless (is_perl55) {
60             # This will elicit a warning on 5.005_03 about [: :] being reserved unless
61             # I cheat
62 209         371 my $cheat = '([[:^print:]])';
63              
64 209         325 if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike.
65             s/$cheat/sprintf "\\%03o", ord $1/ge;
66             } else {
67 209         500 s/([^\0-\177])/sprintf "\\%03o", ord $1/ge;
  21         140  
68             }
69              
70 209         732 s/$cheat/sprintf "\\%03o", ord $1/ge;
  0         0  
71             } else {
72             require POSIX;
73             s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
74             }
75 209         691 $_;
76             }
77              
78             =item perl_stringify NAME
79              
80             A function which returns a 7 bit ASCII correctly \ escaped version of the
81             string passed suitable for a perl "" string.
82              
83             =cut
84              
85             # Hopefully make a happy perl identifier.
86             sub perl_stringify {
87 71     71 1 161 local $_ = shift;
88 71 50       152 return unless defined $_;
89 71         163 s/\\/\\\\/g;
90 71         229 s/([\"\'])/\\$1/g; # Grr. fix perl mode.
91 71         143 s/\n/\\n/g; # Ensure newlines don't end up in octal
92 71         116 s/\r/\\r/g;
93 71         113 s/\t/\\t/g;
94 71         118 s/\f/\\f/g;
95 71         118 s/\a/\\a/g;
96 71         108 unless (is_perl55) {
97             # This will elicit a warning on 5.005_03 about [: :] being reserved unless
98             # I cheat
99 71         127 my $cheat = '([[:^print:]])';
100 71         113 if (is_sane_perl) {
101 71         103 if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike.
102             s/$cheat/sprintf "\\x{%X}", ord $1/ge;
103             } else {
104 71         222 s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
  20         114  
105             }
106             } else {
107             # Grr 5.6.1. And I don't think I can use utf8; to force the regexp
108             # because 5.005_03 will fail.
109             # This is grim, but I also can't split on //
110             my $copy;
111             foreach my $index (0 .. length ($_) - 1) {
112             my $char = substr ($_, $index, 1);
113             $copy .= ($char le "\177") ? $char : sprintf "\\x{%X}", ord $char;
114             }
115             $_ = $copy;
116             }
117 71         238 s/$cheat/sprintf "\\%03o", ord $1/ge;
  0         0  
118             } else {
119             # Turns out "\x{}" notation only arrived with 5.6
120             s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge;
121             require POSIX;
122             s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
123             }
124 71         263 $_;
125             }
126              
127             1;
128             __END__