<?xml version="1.0" encoding="windows-1252"?>
<node id="934562" title="string plus length typemap hack" created="2011-10-29 03:38:08" updated="2011-10-29 03:38:08">
<type id="120">
perlmeditation</type>
<author id="863652">
patcat88</author>
<data>
<field name="doctext">
Doesn't it suck that if you want a string pointer, and its length, in XS, you need to get the SV * and do the SvPV manually each time? Fear no more. Below typemap will give you the char * pointer, and the length. Always.
&lt;code&gt;
void
Foo( str )
     char * str
PPCODE:
     Foo(str, strLen);
&lt;/code&gt;
Change the typemap entry to your liking. Also note caching SV * to avoid numerous ST macros in SvPV macro. Who wants to calculate the same thing half a dozen times? Also less noise in your debugger with the now useless SV * being gone.
&lt;code&gt;
T_STR_AND_LEN
    ".(unshift(@line, (
    'PREINIT:',
    '    unsigned long '.$var.'Len;',
    'INPUT:')),'').
    "{
        SV * TmpSV = $arg;
        $var = ($type) SvPV( TmpSV , ${\$var}Len);
    };
&lt;/code&gt;
Warning, you must use PREINIT for your XSUBs. Lazy coders shall die.
&lt;br&gt;&lt;br&gt;
edited: spelling</field>
</data>
</node>
