package ExtUtils
::Constant
::Utils
;
use vars
qw($VERSION @EXPORT_OK @ISA $is_perl56);
@EXPORT_OK = qw(C_stringify perl_stringify);
$is_perl56 = ($] < 5.007 && $] > 5.005_50
);
ExtUtils::Constant::Utils - helper functions for ExtUtils::Constant
use ExtUtils::Constant::Utils qw (C_stringify);
$C_code = C_stringify $stuff;
ExtUtils::Constant::Utils packages up utility subroutines used by
ExtUtils::Constant, ExtUtils::Constant::Base and derived classes. All its
functions are explicitly exportable.
A function which returns a 7 bit ASCII correctly \ escaped version of the
string passed suitable for C's "" or ''. It will die if passed Unicode
# Hopefully make a happy C identifier.
return unless defined $_;
confess
"Wide character in '$_' intended as a C identifier"
if tr/\0-\377// != length;
# grr 5.6.1 moreso because its regexps will break on data that happens to
# be utf8, which includes my 8 bit test cases.
$_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if $is_perl56;
s/([\"\'])/\\$1/g; # Grr. fix perl mode.
s/\n/\\n/g; # Ensure newlines don't end up in octal
s/([^\0-\177])/sprintf "\\%03o", ord $1/ge;
# This will elicit a warning on 5.005_03 about [: :] being reserved unless
my $cheat = '([[:^print:]])';
s/$cheat/sprintf "\\%03o", ord $1/ge;
s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
=item perl_stringify NAME
A function which returns a 7 bit ASCII correctly \ escaped version of the
string passed suitable for a perl "" string.
# Hopefully make a happy perl identifier.
return unless defined $_;
s/([\"\'])/\\$1/g; # Grr. fix perl mode.
s/\n/\\n/g; # Ensure newlines don't end up in octal
s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
# Grr 5.6.1. And I don't think I can use utf8; to force the regexp
# because 5.005_03 will fail.
# This is grim, but I also can't split on //
foreach my $index (0 .. length ($_) - 1) {
my $char = substr ($_, $index, 1);
$copy .= ($char le "\177") ?
$char : sprintf "\\x{%X}", ord $char;
# This will elicit a warning on 5.005_03 about [: :] being reserved unless
my $cheat = '([[:^print:]])';
s/$cheat/sprintf "\\%03o", ord $1/ge;
# Turns out "\x{}" notation only arrived with 5.6
s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge;
s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
Nicholas Clark <nick@ccl4.org> based on the code in C<h2xs> by Larry Wall and