package ExtUtils
::Constant
;
use vars qw
(@ISA $VERSION @EXPORT_OK %EXPORT_TAGS);
ExtUtils::Constant - generate XS code to import C header constants
use ExtUtils::Constant qw (WriteConstants);
NAMES => [qw(FOO BAR BAZ)],
# Generates wrapper code to make the values of the constants FOO BAR BAZ
ExtUtils::Constant facilitates generating C and XS wrapper code to allow
perl modules to AUTOLOAD constants defined in C library header files.
It is principally used by the C<h2xs> utility, on which this code is based.
It doesn't contain the routines to scan header files to extract these
Generally one only needs to call the C<WriteConstants> function, and then
in the C section of C<Foo.xs>
in the XS section of C<Foo.xs>.
For greater flexibility use C<constant_types()>, C<C_constant> and
C<XS_constant>, with which C<WriteConstants> is implemented.
Currently this module understands the following types. h2xs may only know
a subset. The sizes of the numeric types are chosen by the C<Configure>
signed integer, at least 32 bits.
unsigned integer, the same size as I<IV>
floating point type, probably C<double>, possibly C<long double>
NUL terminated string, length will be determined with C<strlen>
A fixed length thing, given as a [pointer, length] pair. If you know the
length of a string at compile time you may use this instead of I<PV>
Truth. (C<PL_sv_yes>) The value is not needed (and ignored).
Defined Falsehood. (C<PL_sv_no>) The value is not needed (and ignored).
C<undef>. The value of the macro is not needed.
eval "use warnings; 1" or die $@
;
use Carp
qw(croak cluck);
use ExtUtils
::Constant
::Utils
qw(C_stringify);
use ExtUtils
::Constant
::XS
qw(%XS_Constant %XS_TypeSet);
%EXPORT_TAGS = ( 'all' => [ qw(
XS_constant constant_types return_clause memEQ_clause C_stringify
C_constant autoload WriteConstants WriteMakefileSnippet
@EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
A function returning a single scalar with C<#define> definitions for the
constants used internally between the generated C and XS functions.
ExtUtils
::Constant
::XS
->header();
cluck
"ExtUtils::Constant::memEQ_clause is deprecated";
ExtUtils
::Constant
::XS
->memEQ_clause({name
=>$_[0], checked_at
=>$_[1],
cluck
"ExtUtils::Constant::return_clause is deprecated";
ExtUtils
::Constant
::XS
->return_clause({indent
=>$indent}, @_);
cluck
"ExtUtils::Constant::switch_clause is deprecated";
ExtUtils
::Constant
::XS
->switch_clause({indent
=>$indent, comment
=>$comment},
my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
ExtUtils
::Constant
::XS
->C_constant({package => $package, subname
=> $subname,
default_type
=> $default_type,
types
=> $what, indent
=> $indent,
breakout
=> $breakout}, @items);
=item XS_constant PACKAGE, TYPES, SUBNAME, C_SUBNAME
A function to generate the XS code to implement the perl subroutine
I<PACKAGE>::constant used by I<PACKAGE>::AUTOLOAD to load constants.
This XS code is a wrapper around a C subroutine usually generated by
C<C_constant>, and usually named C<constant>.
I<TYPES> should be given either as a comma separated list of types that the
C subroutine C<constant> will generate or as a reference to a hash. It should
be the same list of types as C<C_constant> was given.
[Otherwise C<XS_constant> and C<C_constant> may have different ideas about
the number of parameters passed to the C function C<constant>]
You can call the perl visible subroutine something other than C<constant> if
you give the parameter I<SUBNAME>. The C subroutine it calls defaults to
the name of the perl visible subroutine, unless you give the parameter
# Convert line of the form IV,UV,NV to hash
$what = {map {$_ => 1} split /,\s*/, ($what)};
my $params = ExtUtils
::Constant
::XS
->params ($what);
dXSTARG; /* Faster if we have it. */
$xs .= " /* IV\t\tiv;\tUncomment this if you need to return IVs */\n";
$xs .= " /* NV\t\tnv;\tUncomment this if you need to return NVs */\n";
$xs .= " const char *pv;\n";
" /* const char\t*pv;\tUncomment this if you need to return PVs */\n";
const char
* s
= SvPV
(sv
, len
);
if ($params->{IV
} xor $params->{NV
}) {
/* Change this to
$C_subname(aTHX_ s
, len
, &iv
, &nv
);
if you need to
return both NVs
and IVs
*/
$xs .= " type = $C_subname(aTHX_ s, len";
$xs .= ', utf8' if $params->{''};
$xs .= ', &iv' if $params->{IV
};
$xs .= ', &nv' if $params->{NV
};
$xs .= ', &pv' if $params->{PV
};
$xs .= ', &sv' if $params->{SV
};
/* Return
1 or 2 items
. First is error message
, or undef if no error
.
Second
, if present
, is found value
*/
case PERL_constant_NOTFOUND
:
sv
= sv_2mortal
(newSVpvf
("%s is not a valid $package macro", s
));
case PERL_constant_NOTDEF
:
sv
= sv_2mortal
(newSVpvf
(
"Your vendor has not defined $package macro %s, used", s
));
foreach $type (sort keys %XS_Constant) {
# '' marks utf8 flag needed.
$xs .= "\t/* Uncomment this if you need to return ${type}s\n"
$xs .= " case PERL_constant_IS$type:\n";
if (length $XS_Constant{$type}) {
# Do nothing. return (), which will be correctly interpreted as
unless ($what->{$type}) {
chop $xs; # Yes, another need for chop not chomp.
sv
= sv_2mortal
(newSVpvf
(
"Unexpected return type %d while processing $package macro %s, used",
=item autoload PACKAGE, VERSION, AUTOLOADER
A function to generate the AUTOLOAD subroutine for the module I<PACKAGE>
I<VERSION> is the perl version the code should be backwards compatible with.
It defaults to the version of perl running the subroutine. If I<AUTOLOADER>
is true, the AUTOLOAD subroutine falls back on AutoLoader::AUTOLOAD for all
names that the constant() routine doesn't recognise.
# ' # Grr. syntax highlighters that don't grok pod.
my ($module, $compat_version, $autoloader) = @_;
croak
"Can't maintain compatibility back as far as version $compat_version"
my $func = "sub AUTOLOAD {\n"
. " # This AUTOLOAD is used to 'autoload' constants from the constant()\n"
$func .= " If a constant is not found then control is passed\n"
. " # to the AUTOLOAD in AutoLoader." if $autoloader;
" our \$AUTOLOAD;\n" if ($compat_version >= 5.006);
(\$constname = \$AUTOLOAD) =~ s/.*:://;
croak "&${module}::constant not defined" if \$constname eq 'constant';
my (\$error, \$val) = constant(\$constname);
if ($error =~ /is not a valid/) {
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
" if (\$error) { croak \$error; }\n";
# Fixed between 5.005_53 and 5.005_61
#XXX if ($] >= 5.00561) {
#XXX *$AUTOLOAD = sub () { $val };
*$AUTOLOAD = sub { $val };
=item WriteMakefileSnippet
WriteMakefileSnippet ATTRIBUTE =E<gt> VALUE [, ...]
A function to generate perl code for Makefile.PL that will regenerate
the constant subroutines. Parameters are named as passed to C<WriteConstants>,
with the addition of C<INDENT> to specify the number of leading spaces
Currently only C<INDENT>, C<NAME>, C<DEFAULT_TYPE>, C<NAMES>, C<C_FILE> and
C<XS_FILE> are recognised.
sub WriteMakefileSnippet
{
my $indent = $args{INDENT
} || 2;
ExtUtils::Constant::WriteConstants(
DEFAULT_TYPE => '$args{DEFAULT_TYPE}',
foreach (qw
(C_FILE XS_FILE
)) {
next unless exists $args{$_};
$result .= sprintf " %-12s => '%s',\n",
$result =~ s/^/' 'x$indent/gem;
return ExtUtils
::Constant
::XS
->dump_names({default_type
=>$args{DEFAULT_TYPE
},
=item WriteConstants ATTRIBUTE =E<gt> VALUE [, ...]
Writes a file of C code and a file of XS code which you should C<#include>
and C<INCLUDE> in the C and XS sections respectively of your module's XS
code. You probably want to do this in your C<Makefile.PL>, so that you can
easily edit the list of constants without touching the rest of your module.
The attributes supported are
Name of the module. This must be specified
The default type for the constants. If not specified C<IV> is assumed.
The names of the constants are grouped by length. Generate child subroutines
for each group with this number or more names in.
An array of constants' names, either scalars containing names, or hashrefs
as detailed in L<"C_constant">.
The name of the file to write containing the C code. The default is
C<const-c.inc>. The C<-> in the name ensures that the file can't be
mistaken for anything related to a legitimate perl package name, and
not naming the file C<.c> avoids having to override Makefile.PL's
The name of the file to write containing the XS code. The default is
The perl visible name of the XS subroutine generated which will return the
constants. The default is C<constant>.
The name of the C subroutine generated which will return the constants.
The default is I<SUBNAME>. Child subroutines have C<_> and the name
length appended, so constants with 10 character names would be in
C<constant_10> with the default I<XS_SUBNAME>.
XS_FILE
=> 'const-xs.inc',
$ARGS{C_SUBNAME
} ||= $ARGS{SUBNAME
}; # No-one sane will have C_SUBNAME eq '0'
croak
"Module name not specified" unless length $ARGS{NAME
};
# We need these little games, rather than doing things unconditionally,
# because we're used in core Makefile.PLs before IO is available (needed
# by filehandle), but also we want to work on older perls where undefined
# scalars do not automatically turn into anonymous file handles.
$c_fh = FileHandle
->new();
$xs_fh = FileHandle
->new();
open $c_fh, ">$ARGS{C_FILE}" or die "Can't open $ARGS{C_FILE}: $!";
open $xs_fh, ">$ARGS{XS_FILE}" or die "Can't open $ARGS{XS_FILE}: $!";
# As this subroutine is intended to make code that isn't edited, there's no
# need for the user to specify any types that aren't found in the list of
print $c_fh constant_types
(); # macro defs
# indent is still undef. Until anyone implements indent style rules with it.
foreach (ExtUtils
::Constant
::XS
->C_constant({package => $ARGS{NAME
},
subname
=> $ARGS{C_SUBNAME
},
breakout
=> $ARGS{BREAKOUT_AT
}},
print $c_fh $_, "\n"; # C constant subs
print $xs_fh XS_constant
($ARGS{NAME
}, $types, $ARGS{XS_SUBNAME
},
close $c_fh or warn "Error closing $ARGS{C_FILE}: $!";
close $xs_fh or warn "Error closing $ARGS{XS_FILE}: $!";
Nicholas Clark <nick@ccl4.org> based on the code in C<h2xs> by Larry Wall and