perl-xs

Guides writing and debugging Perl XS code that wraps C libraries at the Perl/C boundary.

1|Updated Mar 29, 2026
One-click install
npx skills add https://github.com/Getty/p5-alien-libssh --skill perl-xs-getty
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: perl-xs
Source: https://github.com/Getty/p5-alien-libssh/tree/main/.claude/skills/perl-xs
Command: npx skills add https://github.com/Getty/p5-alien-libssh --skill perl-xs-getty

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing XS — the glue between Perl and C — involves subtle failure modes: segfaults from wrong refcounts, leaks from skipped OUTPUT sections, cryptic xsubpp errors from typemap escaping, and silent wrong answers from NULL handles. This Skill provides the operational knowledge to write correct XS bindings and diagnose these failures systematically. ## Core Features & Use Cases - Object and memory management: Attach C pointers to blessed SVs via sv_magicext with per-type MGVTBL free hooks, handle child/parent refcount chains, and use generation counters when C libraries free objects behind your back. - Typemap authoring: Write INPUT/OUTPUT conversion templates with correct TAB indentation, quote escaping, and per-type vtable checks that croak instead of segfaulting. - Build and test discipline: Regenerate ppport.h with NEED_ defines, read generated C from ExtUtils::ParseXS, and test crashes in forked children, leaks with Test::LeakTrace and valgrind, and blocking calls with alarm. - Use Case: You are wrapping a C library like libssh as a Perl distribution and a test segfaults when a parent object is undefined — the Skill explains the SvRV(ST(0)) refcount rule and how to test all three ways of losing a variable. ## Quick Start Ask the AI to help write or debug an XS file, typemap, or Makefile.PL for wrapping a C library in Perl, or to diagnose a segfault, leak, or xsubpp compile error in existing XS code.

Frequently Asked Questions about perl-xs

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I wrap a C library as a Perl module with XS?

Write an .xs file with the standard preamble (EXTERN.h, perl.h, XSUB.h, ppport.h), declare MODULE/PACKAGE sections, and define XSUBs whose arguments convert through a per-distribution typemap. Store C pointers as magic on blessed SVs using sv_magicext with a per-type MGVTBL whose svt_free hook releases the C resource.

How do I attach a C pointer to a Perl object in XS?

Use sv_magicext to attach the pointer as PERL_MAGIC_ext magic on the blessed referent, with a static const MGVTBL unique to that type. The vtable address doubles as the type check via mg_findext, and its svt_free hook acts as the destructor, so no Perl DESTROY method is needed.

Why does my XS code segfault when I undef a parent object?

The child object likely incremented the refcount on ST(0), the reference, instead of SvRV(ST(0)), the blessed referent carrying the magic. Holding the reference does not keep the referent alive after undef or reassignment, so the child's C pointer dangles. Increment the referent and test all three ways of losing the variable.

Why does xsubpp generate C code with syntax errors I never wrote?

The usual cause is an unescaped double quote in a typemap INPUT or OUTPUT template, which xsubpp evaluates as a Perl double-quoted string. Write quotes meant for the generated C as \". Spaces instead of literal TABs for template indentation cause similar silent parse failures.

How do I test XS code for memory leaks and crashes?

Run crash-prone scenarios in forked children so a segfault fails one test instead of killing prove. Use Test::LeakTrace's no_leaks_ok for Perl-level leaks and valgrind with PERL_DESTRUCT_LEVEL=2 for C-level leaks. Guard blocking C calls with alarm so hangs become reported failures.

When should I avoid T_PTROBJ in a Perl typemap?

Avoid T_PTROBJ whenever the wrapped pointer needs cleanup or type safety. It stores the pointer with sv_setref_pv, offering no free hook and no vtable-based type check, so a hand-blessed hashref passes the boundary and segfaults. Use sv_magicext with a dedicated MGVTBL instead.