$VERSION = '0.14';
=head1 NAME
ExtUtils::Constant - generate XS code to import C header constants
=head1 SYNOPSIS
use ExtUtils::Constant qw (WriteConstants);
WriteConstants(
NAME => 'Foo',
NAMES => [qw(FOO BAR BAZ)],
);
# Generates wrapper code to make the values of the constants FOO BAR BAZ
# available to perl
=head1 DESCRIPTION
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
constants.
=head1 USAGE
Generally one only needs to call the C<WriteConstants> function, and then
#include "const-c.inc"
in the C section of C<Foo.xs>
INCLUDE const-xs.inc
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>
script at compile time.
=over 4
=item IV
signed integer, at least 32 bits.
=item UV
unsigned integer, the same size as I<IV>
=item NV
floating point type, probably C<double>, possibly C<long double>
=item PV
NUL terminated string, length will be determined with C<strlen>
=item PVN
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>
=item SV
A B<mortal> SV.
=item YES
Truth. (C<PL_sv_yes>) The value is not needed (and ignored).
=item NO
Defined Falsehood. (C<PL_sv_no>) The value is not needed (and ignored).
=item UNDEF
C<undef>. The value of the macro is not needed.
=back
=head1 FUNCTIONS
=over 4
=cut
if ($] >= 5.006) {
eval "use warnings; 1" or die $@;
}
use strict;
use vars '$is_perl56';
use Carp;
use Exporter;
@ISA = 'Exporter';
%EXPORT_TAGS = ( 'all' => [ qw(
) ] );
# '' is used as a flag to indicate non-ascii macro names, and hence the need
%XS_Constant = (
'' => '',
IV => 'PUSHi(iv)',
UV => 'PUSHu((UV)iv)',
NV => 'PUSHn(nv)',
PV => 'PUSHp(pv, strlen(pv))',
PVN => 'PUSHp(pv, iv)',
SV => 'PUSHs(sv)',
YES => 'PUSHs(&PL_sv_yes)',
NO => 'PUSHs(&PL_sv_no)',
);
%XS_TypeSet = (
IV => '*iv_return =',
UV => '*iv_return = (IV)',
NV => '*nv_return =',
PV => '*pv_return =',
SV => '*sv_return = ',
YES => undef,
NO => undef,
UNDEF => undef,
);
=item C_stringify NAME
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
characters.
=cut
# Hopefully make a happy C identifier.
sub C_stringify {
local $_ = shift;
return unless defined $_;
# grr 5.6.1
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.
s/\\/\\\\/g;
s/([\"\'])/\\$1/g; # Grr. fix perl mode.
s/\n/\\n/g; # Ensure newlines don't end up in octal
s/\r/\\r/g;
s/\t/\\t/g;
s/\f/\\f/g;
s/\a/\\a/g;
s/([^\0-\177])/sprintf "\\%03o", ord $1/ge;
unless ($] < 5.006) {
# This will elicit a warning on 5.005_03 about [: :] being reserved unless
# I cheat
my $cheat = '([[:^print:]])';
} else {
require POSIX;
}
$_;
}
=item perl_stringify NAME
A function which returns a 7 bit ASCII correctly \ escaped version of the
string passed suitable for a perl "" string.
=cut
# Hopefully make a happy perl identifier.
sub perl_stringify {
local $_ = shift;
return unless defined $_;
s/\\/\\\\/g;
s/([\"\'])/\\$1/g; # Grr. fix perl mode.
s/\n/\\n/g; # Ensure newlines don't end up in octal
s/\r/\\r/g;
s/\t/\\t/g;
s/\f/\\f/g;
s/\a/\\a/g;
unless ($] < 5.006) {
if ($] > 5.007) {
s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
} else {
# 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 //
my $copy;
}
$_ = $copy;
}
# This will elicit a warning on 5.005_03 about [: :] being reserved unless
# I cheat
my $cheat = '([[:^print:]])';
} else {
# Turns out "\x{}" notation only arrived with 5.6
s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge;
require POSIX;
}
$_;
}
=item constant_types
A function returning a single scalar with C<#define> definitions for the
constants used internally between the generated C and XS functions.
=cut
sub constant_types () {
my $start = 1;
my @lines;
foreach (sort keys %XS_Constant) {
next if $_ eq '';
}
push @lines, << 'EOT';
#ifndef NVTYPE
#endif
#ifndef aTHX_
#define aTHX_ /* 5.6 or later define this for threading support. */
#endif
#ifndef pTHX_
#define pTHX_ /* 5.6 or later define this for threading support. */
#endif
return join '', @lines;
}
=item memEQ_clause NAME, CHECKED_AT, INDENT
A function to return a suitable C C<if> statement to check whether I<NAME>
is equal to the C variable C<name>. If I<CHECKED_AT> is defined, then it
is used to avoid C<memEQ> for short names, or to generate a comment to
highlight the position of the character in the C<switch> statement.
If I<CHECKED_AT> is a reference to a scalar, then instead it gives
the characters pre-checked at the beginning, (and the number of chars by
which the C variable name has been advanced. These need to be chopped from
the front of I<NAME>).
=cut
sub memEQ_clause {
# if (memEQ(name, "thingy", 6)) {
# Which could actually be a character comparison or even ""
my $front_chop;
if (ref $checked_at) {
# regexp won't work on 5.6.1 without use utf8; in turn that won't work
# on 5.005_03.
undef $checked_at;
}
if ($len < 2) {
# We didn't switch, drop through to the code for the 2 character string
$checked_at = 1;
}
my $check;
if ($checked_at == 1) {
$check = 0;
} elsif ($checked_at == 0) {
$check = 1;
}
if (defined $check) {
return $indent . "if (name[$check] == '$char') {\n";
}
}
return $indent . "if (name[0] == '$char1' && name[1] == '$char2') {\n";
}
return $indent . "if (name[0] == '$char1' && name[2] == '$char2') {\n";
}
my $pointer = '^';
if ($have_checked_last) {
# Checked at the last character, so no need to memEQ it.
$len--;
}
# Put a little ^ under the letter we checked at
# Screws up for non printable and non-7 bit stuff, but that's too hard to
# get right.
if (defined $checked_at) {
} elsif (defined $front_chop) {
}
return $body;
}
=item assign INDENT, TYPE, PRE, POST, VALUE...
A function to return a suitable assignment clause. If I<TYPE> is aggregate
(eg I<PVN> expects both pointer and length) then there should be multiple
I<VALUE>s for the components. I<PRE> and I<POST> if defined give snippets
of C code to proceed and follow the assignment. I<PRE> will be at the start
of a block, so variables may be defined in it.
=cut
# Hmm. value undef to to NOTDEF? value () to do NOTFOUND?
sub assign {
my $indent = shift;
my $type = shift;
my $pre = shift;
my $post = shift || '';
my $clause;
my $close;
if ($pre) {
chomp $pre;
$clause .= "\n";
$close = "$indent}\n";
$indent .= " ";
}
confess "undef \$type" unless defined $type;
if (ref $typeset) {
die "Type $type is aggregate, but only single value given"
if @_ == 1;
foreach (0 .. $#$typeset) {
}
} elsif (defined $typeset) {
die "Aggregate value given for type $type"
if @_ > 1;
}
chomp $post;
if (length $post) {
$clause .= "$post";
$clause .= "\n";
}
$clause .= "${indent}return PERL_constant_IS$type;\n";
return $clause;
}
=item return_clause
return_clause ITEM, INDENT
A function to return a suitable C<#ifdef> clause. I<ITEM> is a hashref
(as passed to C<C_constant> and C<match_clause>. I<INDENT> is the number
of spaces to indent, defaulting to 6.
=cut
sub return_clause ($$) {
##ifdef thingy
# *iv_return = thingy;
# return PERL_constant_ISIV;
##else
# return PERL_constant_NOTDEF;
##endif
unless ($type) {
# use Data::Dumper; print STDERR Dumper ($item);
confess "undef \$type";
}
my $clause;
##ifdef thingy
if (ref $macro) {
} elsif ($macro ne "1") {
$clause = "#ifdef $macro\n";
}
# *iv_return = thingy;
# return PERL_constant_ISIV;
##else
$clause .= "#else\n";
# return PERL_constant_NOTDEF;
if (!defined $default) {
$clause .= "${indent}return PERL_constant_NOTDEF;\n";
} else {
}
##endif
if (ref $macro) {
} else {
$clause .= "#endif\n";
}
}
return $clause;
}
=pod
XXX document me
=cut
sub match_clause {
# $offset defined if we have checked an offset.
my $body = '';
if (ref $item eq 'ARRAY') {
confess "$item is $either expecting hashref in [0] || [1]"
unless ref $either eq 'HASH';
} else {
confess "$item->{name} has utf8 flag '$item->{utf8}', should be false"
if $item->{utf8};
$inner_indent = $indent;
}
if ($yes) {
} elsif ($no) {
}
if ($either) {
}
} else {
}
}
=item switch_clause INDENT, NAMELEN, ITEMHASH, ITEM...
An internal function to generate a suitable C<switch> clause, called by
C<C_constant> I<ITEM>s are in the hash ref format as given in the description
of C<C_constant>, and must all have the names of the same length, given by
I<NAMELEN> (This is not checked). I<ITEMHASH> is a reference to a hash,
keyed by name, values being the hashrefs in the I<ITEM> list.
(No parameters are modified, and there can be keys in the I<ITEMHASH> that
are not in the list of I<ITEM>s without causing problems).
=cut
sub switch_clause {
if ($comment) {
}
my @safe_names = @names;
foreach (@safe_names) {
confess sprintf "Name '$_' is length %d, not $namelen", length
unless length == $namelen;
# Argh. 5.6.1
# Ensure that the enclosing C comment doesn't end
# by turning */ into *" . "/
s!\*\/!\*"."/!gs;
# gcc -Wall doesn't like finding /* inside a comment
s!\/\*!/"."\*!gs;
}
# Figure out what to switch on.
# (RMS, Spread of jump table, Position, Hashref)
# Prefer the last character over the others. (As it lets us shortern the
# memEQ clause at no cost).
my %spread;
if ($is_perl56) {
# Need proper Unicode preserving hash keys for bytes in range 128-255
# here too, for some reason. grr 5.6.1 yet again.
tie %spread, 'ExtUtils::Constant::Aaargh56Hash';
}
foreach (@names) {
my $char = substr $_, $i, 1;
# warn "$_ $char";
}
# I'm going to pick the character to split on that minimises the root
# mean square of the number of names in each case. Normally this should
# be the one with the most keys, but it may pick a 7 where the 8 has
# one long linear search. I'm not sure if RMS or just sum of squares is
# actually better.
# $max and $min are for the tie-breaker if the root mean squares match.
# Assuming that the compiler may be building a jump table for the
# switch() then try to minimise the size of that jump table.
# Finally use < not <= so that if it still ties the earliest part of
# the string wins. Because if that passes but the memEQ fails, it may
# only need the start of the string to bin the choice.
# I think. But I'm micro-optimising. :-)
# OK. Trump that. Now favour the last character of the string, before the
# rest.
my $ss;
}
}
confess "Internal error. Failed to pick a switch point for @names"
unless defined $best[2];
# use Data::Dumper; print Dumper (@best);
if ($do_front_chop) {
} else {
}
confess sprintf "'$char' is %d bytes long, not 1", length $char
if length ($char) != 1;
# warn "You are here";
if ($do_front_chop) {
} else {
}
}
}
return $body;
}
=item params WHAT
An internal function. I<WHAT> should be a hashref of types the constant
function will return. I<params> returns a hashref keyed IV NV PV SV to show
which combination of pointers will be needed in the C argument list.
=cut
sub params {
my $what = shift;
foreach (sort keys %$what) {
warn "ExtUtils::Constant doesn't know how to handle values of type $_" unless defined $XS_Constant{$_};
}
my $params = {};
return $params;
}
=item dump_names
dump_names DEFAULT_TYPE, TYPES, INDENT, OPTIONS, ITEM...
An internal function to generate the embedded perl code that will regenerate
the constant subroutines. I<DEFAULT_TYPE>, I<TYPES> and I<ITEM>s are the
same as for C_constant. I<INDENT> is treated as number of spaces to indent
by. I<OPTIONS> is a hashref of options. Currently only C<declare_types> is
recognised. If the value is true a C<$types> is always declared in the perl
code generated, if defined and false never declared, and if undefined C<$types>
is only declared if the values in I<TYPES> as passed in cannot be inferred from
I<DEFAULT_TYPES> and the I<ITEM>s.
=cut
sub dump_names {
my $result;
foreach (@items) {
my $type;
if (ref $_) {
if ($_->{utf8}) {
# For simplicity always skip the bytes case, and reconstitute this entry
# from its utf8 twin.
next if $_->{utf8} eq 'no';
# Copy the hashref, as we don't want to mess with the caller's hashref.
$_ = {%$_};
unless ($is_perl56) {
} else {
}
delete $_->{utf8};
}
} else {
$_ = {name=>$_};
$type = $default_type;
}
$used_types{$type}++;
if ($type eq $default_type
# grr 5.6.1
and !defined ($_->{def_post})) {
# It's the default type, and the name consists only of A-Za-z0-9_
} else {
push @complex, $_;
}
}
if (!defined $declare_types) {
# Do they pass in any types we weren't already using?
foreach (keys %$what) {
next if $used_types{$_};
$declare_types++; # Found one in $what that wasn't used.
last; # And one is enough to terminate this loop
}
}
if ($declare_types) {
}
if (@complex) {
my $line = ",\n$indent {name=>\"$name\"";
if (defined $value) {
if (ref $value) {
$line .= ", $thing=>[\""
} else {
}
}
}
$line .= "}";
# Ensure that the enclosing C comment doesn't end
# by turning */ into *" . "/
# gcc -Wall doesn't like finding /* inside a comment
}
}
$result .= ");\n";
$result;
}
=item dogfood
dogfood PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
An internal function to generate the embedded perl code that will regenerate
the constant subroutines. Parameters are the same as for C_constant.
=cut
sub dogfood {
= @_;
my $result = <<"EOT";
perl -x
#!$^X -w
$result .= <<'EOT';
print constant_types(); # macro defs
$result .=
"foreach (C_constant (\"$package\", '$subname', '$default_type', \$types, ";
# The form of the indent parameter isn't defined. (Yet)
if (defined $indent) {
} else {
$result .= 'undef';
}
print $_, "\n"; # C constant subs
}
print "#### XS Section:\n";
*/
';
$result;
}
=item C_constant
C_constant PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
A function that returns a B<list> of C subroutine definitions that return
the value and type of constants when passed the name by the XS wrapper.
I<ITEM...> gives a list of constant names. Each can either be a string,
which is taken as a C macro name, or a reference to a hash with the following
keys
=over 8
=item name
The name of the constant, as seen by the perl code.
=item type
The type of the constant (I<IV>, I<NV> etc)
=item value
A C expression for the value of the constant, or a list of C expressions if
the type is aggregate. This defaults to the I<name> if not given.
=item macro
The C pre-processor macro to use in the C<#ifdef>. This defaults to the
I<name>, and is mainly used if I<value> is an C<enum>. If a reference an
array is passed then the first element is used in place of the C<#ifdef>
line, and the second element in place of the C<#endif>. This allows
pre-processor constructions such as
#if defined (foo)
#if !defined (bar)
...
#endif
#endif
to be used to determine if a constant is to be defined.
A "macro" 1 signals that the constant is always defined, so the C<#if>/C<#endif>
test is omitted.
=item default
Default value to use (instead of C<croak>ing with "your vendor has not
defined...") to return if the macro isn't defined. Specify a reference to
an array with type followed by value(s).
=item pre
C code to use before the assignment of the value of the constant. This allows
you to use temporary variables to extract a value from part of a C<struct>
and return this as I<value>. This C code is places at the start of a block,
so you can declare variables in it.
=item post
C code to place between the assignment of value (to a temporary) and the
return from the function. This allows you to clear up anything in I<pre>.
Rarely needed.
=item def_pre
=item def_post
Equivalents of I<pre> and I<post> for the default value.
=item utf8
Generated internally. Is zero or undefined if name is 7 bit ASCII,
"no" if the name is 8 bit (and so should only match if SvUTF8() is false),
"yes" if the name is utf8 encoded.
The internals automatically clone any name with characters 128-255 but none
256+ (ie one that could be either in bytes or utf8) into a second entry
which is utf8 encoded.
=back
I<PACKAGE> is the name of the package, and is only used in comments inside the
generated C code.
The next 5 arguments can safely be given as C<undef>, and are mainly used
for recursion. I<SUBNAME> defaults to C<constant> if undefined.
I<DEFAULT_TYPE> is the type returned by C<ITEM>s that don't specify their
type. In turn it defaults to I<IV>. 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. I<DEFAULT_TYPE> will be added to the list if not
present, as will any types given in the list of I<ITEM>s. The resultant list
should be the same list of types that C<XS_constant> is given. [Otherwise
C<XS_constant> and C<C_constant> may differ in the number of parameters to the
constant function. I<INDENT> is currently unused and ignored. In future it may
be used to pass in information used to change the C indentation style used.]
The best way to maintain consistency is to pass in a hash reference and let
this function update it.
I<BREAKOUT> governs when child functions of I<SUBNAME> are generated. If there
are I<BREAKOUT> or more I<ITEM>s with the same length of name, then the code
to switch between them is placed into a function named I<SUBNAME>_I<LEN>, for
example C<constant_5> for names 5 characters long. The default I<BREAKOUT> is
3. A single C<ITEM> is always inlined.
=cut
# The parameter now BREAKOUT was previously documented as:
#
# I<NAMELEN> if defined signals that all the I<name>s of the I<ITEM>s are of
# this length, and that the constant name passed in by perl is checked and
# also of this length. It is used during recursion, and should be C<undef>
# unless the caller has checked all the lengths during code generation, and
# the generated subroutine is only to be called with a name of this length.
#
# As you can see it now performs this function during recursion by being a
# scalar reference.
sub C_constant {
= @_;
$package ||= 'Foo';
$subname ||= 'constant';
# I'm not using this. But a hashref could be used for full formatting without
# breaking this API
# $indent ||= 0;
if (ref $breakout) {
# We are called recursively. We trust @items to be normalised, $what to
# be a hashref, and pinch %$items from our parent to save recalculation.
} else {
if ($is_perl56) {
# Need proper Unicode preserving hash keys.
$items = {};
tie %$items, 'ExtUtils::Constant::Aaargh56Hash';
}
$breakout ||= 3;
$default_type ||= 'IV';
if (!ref $what) {
# Convert line of the form IV,UV,NV to hash
# Figure out what types we're dealing with, and assign all unknowns to the
# default type
}
my @new_items;
if (ref $orig) {
# Make a copy which is a normalised version of the ref passed in.
$type ||= $default_type;
# warn "$key $value";
}
} else {
}
warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$item->{type}};
# tr///c is broken on 5.6.1 for utf8, so my original tr/\0-\177//c
# doesn't work. Upgrade to 5.8
# if ($name !~ tr/\0-\177//c || $] < 5.005_50) {
# No characters outside 7 bit ASCII.
die "Multiple definitions for macro $name";
}
} else {
# No characters outside 8 bit. This is hardest.
confess "Unexpected ASCII definition for macro $name";
}
# Again, 5.6.1 tr broken, so s/5\.6.*/5\.8\.0/;
# if ($name !~ tr/\0-\377//c) {
# if ($] < 5.007) {
# $name = pack "C*", unpack "U*", $name;
# }
$item->{utf8} = 'no';
# Copy item, to create the utf8 variant.
}
# Encode the name as utf8 bytes.
unless ($is_perl56) {
} else {
# warn "Was >$name< " . length ${name};
# warn "Now '${name}' " . length ${name};
}
die "Multiple definitions for macro $name";
}
$item->{utf8} = 'yes';
# We have need for the utf8 flag.
}
}
# use Data::Dumper; print Dumper @items;
}
$body .= ") {\n";
if (defined $namelen) {
# We are a child subroutine. Print the simple description
my $comment = 'When generated this function returned values for the list'
. ' of names given here. However, subsequent manual editing may have'
. ' added or removed some.';
} else {
# We are the top level.
$body .= " /* Initially switch on the length of the name. */\n";
$body .= " switch (len) {\n";
# Need to group names of the same length
my @by_length;
foreach (@items) {
}
foreach my $i (0 .. $#by_length) {
next unless $by_length[$i]; # None of this length
$body .= " case $i:\n";
if (@{$by_length[$i]} == 1) {
if ($only_thing->{utf8}) {
if ($only_thing->{utf8} eq 'yes') {
# With utf8 on flag item is passed in element 0
} else {
# With utf8 off flag item is passed in element 1
}
} else {
}
} else {
# Only use the minimal set of parameters actually needed by the types
# of the names of this length.
my $what = {};
foreach (@{$by_length[$i]}) {
}
$body .= " return ${subname}_$i (aTHX_ name";
$body .= ");\n";
}
$body .= " break;\n";
}
$body .= " }\n";
}
$body .= " return PERL_constant_NOTFOUND;\n}\n";
}
=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
I<C_SUBNAME>.
=cut
sub XS_constant {
my $package = shift;
my $what = shift;
my $subname = shift;
my $C_subname = shift;
$subname ||= 'constant';
if (!ref $what) {
# Convert line of the form IV,UV,NV to hash
}
my $type;
my $xs = <<"EOT";
#ifdef dXSTARG
#else
#endif
int type;
$xs .= " IV iv;\n";
} else {
$xs .= " /* IV\t\tiv;\tUncomment this if you need to return IVs */\n";
}
$xs .= " NV nv;\n";
} else {
$xs .= " /* NV\t\tnv;\tUncomment this if you need to return NVs */\n";
}
$xs .= " const char *pv;\n";
} else {
$xs .=
" /* const char\t*pv;\tUncomment this if you need to return PVs */\n";
}
$xs .= << 'EOT';
if ($params->{''}) {
$xs .= << 'EOT';
}
$xs .= << 'EOT';
$xs .= << "EOT";
}
$xs .= " type = $C_subname(aTHX_ s, len";
$xs .= ");\n";
$xs .= << "EOT";
switch (type) {
break;
"Your vendor has not defined $package macro %s, used", s));
break;
foreach $type (sort keys %XS_Constant) {
# '' marks utf8 flag needed.
next if $type eq '';
$xs .= "\t/* Uncomment this if you need to return ${type}s\n"
$xs .= " case PERL_constant_IS$type:\n";
if (length $XS_Constant{$type}) {
$xs .= << "EOT";
PUSHs(&PL_sv_undef);
$XS_Constant{$type};
} else {
# Do nothing. return (), which will be correctly interpreted as
# (undef, undef)
}
$xs .= " break;\n";
chop $xs; # Yes, another need for chop not chomp.
$xs .= " */\n";
}
}
$xs .= << "EOT";
"Unexpected return type %d while processing $package macro %s, used",
type, s));
}
return $xs;
}
=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.
=cut
# ' # Grr. syntax highlighters that don't grok pod.
sub autoload {
$compat_version ||= $];
croak "Can't maintain compatibility back as far as version $compat_version"
if $compat_version < 5;
my $func = "sub AUTOLOAD {\n"
. " # This AUTOLOAD is used to 'autoload' constants from the constant()\n"
. " # XS function.";
$func .= " If a constant is not found then control is passed\n"
. " # to the AUTOLOAD in AutoLoader." if $autoloader;
$func .= "\n\n"
. " my \$constname;\n";
$func .=
$func .= <<"EOT";
if ($autoloader) {
$func .= <<'EOT';
if ($error) {
goto &AutoLoader::AUTOLOAD;
} else {
croak $error;
}
}
} else {
$func .=
" if (\$error) { croak \$error; }\n";
}
$func .= <<'END';
{
no strict 'refs';
# Fixed between 5.005_53 and 5.005_61
#XXX if ($] >= 5.00561) {
#XXX *$AUTOLOAD = sub () { $val };
#XXX }
#XXX else {
#XXX }
}
goto &$AUTOLOAD;
}
END
return $func;
}
=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
(default 2).
Currently only C<INDENT>, C<NAME>, C<DEFAULT_TYPE>, C<NAMES>, C<C_FILE> and
C<XS_FILE> are recognised.
=cut
sub WriteMakefileSnippet {
my %args = @_;
my $result = <<"EOT";
NAME => '$args{NAME}',
DEFAULT_TYPE => '$args{DEFAULT_TYPE}',
next unless exists $args{$_};
$result .= sprintf " %-12s => '%s',\n",
$_, $args{$_};
}
$result .= <<'EOT';
);
. $result;
}
=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
=over 4
=item NAME
Name of the module. This must be specified
=item DEFAULT_TYPE
The default type for the constants. If not specified C<IV> is assumed.
=item BREAKOUT_AT
The names of the constants are grouped by length. Generate child subroutines
for each group with this number or more names in.
=item NAMES
An array of constants' names, either scalars containing names, or hashrefs
as detailed in L<"C_constant">.
=item C_FILE
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
C<.xs> to C<.c> rules.
=item XS_FILE
The name of the file to write containing the XS code. The default is
C<const-xs.inc>.
=item SUBNAME
The perl visible name of the XS subroutine generated which will return the
constants. The default is C<constant>.
=item C_SUBNAME
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>.
=back
=cut
sub WriteConstants {
my %ARGS =
( # defaults
C_FILE => 'const-c.inc',
XS_FILE => 'const-xs.inc',
SUBNAME => 'constant',
DEFAULT_TYPE => 'IV',
@_);
# 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
# names.
my $types = {};
print $c_fh "\n";
# indent is still undef. Until anyone implements indent style rules with it.
}
close $c_fh or warn "Error closing $ARGS{C_FILE}: $!";
close $xs_fh or warn "Error closing $ARGS{XS_FILE}: $!";
}
# A support module (hack) to provide sane Unicode hash keys on 5.6.x perl
use strict;
use vars '@ISA';
@ISA = 'Tie::StdHash';
#my $a;
# Storing the values as concatenated BER encoded numbers is actually going to
# be terser than using UTF8 :-)
# And the tests are slightly faster. Ops are bad, m'kay
#END {warn "$a accesses";}
1;
=back
=head1 AUTHOR
Nicholas Clark <nick@ccl4.org> based on the code in C<h2xs> by Larry Wall and
others
=cut