読者です 読者をやめる 読者になる 読者になる

XSお勉強メモ3

perl

SV構造体と操作関数の確認用スクリプト。

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"


MODULE = Foo::Bar               PACKAGE = Foo::Bar

PROTOTYPES: DISABLE


void
test_newSV()
CODE:
{
   SV *svPv = sv_2mortal(newSVpvs( "foo" ));
   SV *svIv = sv_2mortal(newSViv( -100 ));
   SV *svUv = sv_2mortal(newSVuv( 100 ));
   SV *svNv = sv_2mortal(newSVnv( 100.123 ));


   XPV *anyPv   = (XPV *)SvANY( svPv );
   XPVIV *anyIv = (XPVIV *)SvANY( svIv );
   XPVUV *anyUv = (XPVUV *)SvANY( svUv );
   XPVNV *anyNv = (XPVNV *)SvANY( svNv );


   STRLEN len;
   char* pv = SvPV( svPv, len );

   int      iv = SvIV( svIv );
   unsigned uv = SvUV( svUv );
   float    nv = SvNV( svNv );
}



void
test_strAPI()
CODE:
{
   SV* str = sv_2mortal(newSV(0));

   // strcpy(dt, src)
   sv_setpv(str, "test");

   // strlen(s)
   STRLEN len = sv_len(str);

    // strncpy(dt, src, n)
   sv_setpvn(str, "test", 2);

   // strcat(dt, str)
   sv_catpv(str, "stst");

    // strncat(dt, str)
   sv_catpvn(str, "1234", 3);

   // sprintf(s, fmt, ...)
   sv_setpvf(str, "%d:%f:%s", 100, 123.123, "test");
}