Alien-libssh
view release on metacpan or search on metacpan
misc/agent-context/.claude/skills/perl-xs/references/build-and-test.md view on Meta::CPAN
A typemap that needs a minimum states `REQUIRE: 3.60` on its first line. Without
that line, a construct like `${type}` expanding `::` to `__` silently produces
uncompilable C on older toolchains.
## Reading the generated C
When a compile error points into generated code, generate it directly and read it:
```bash
perl -MExtUtils::ParseXS -e \
'ExtUtils::ParseXS->new->process_file(filename=>"Foo.xs", output=>"/tmp/gen.c",
typemap=>"typemap")'
```
The `#line` directives map every block back to its source line in the `.xs`, and the
argument-conversion code the typemap produced is right there to compare against what
it was supposed to produce. Keep the file out of the distribution â the `.c` beside
the `.xs` is a build artifact and belongs in `.gitignore`.
## Testing what only XS gets wrong
Perl-level tests cover the API. Three failure modes need their own shape:
**Crashes.** A refcount or lifetime bug segfaults, and a segfault takes `prove` down
instead of failing a test. Run each such scenario in a forked child and assert on
what the child reports:
```perl
use POSIX ();
sub run_in_child {
my ($name, $code, $expect) = @_;
pipe(my $rd, my $wr) or die "pipe: $!";
my $pid = fork() // die "fork: $!";
if (!$pid) { print {$wr} eval { $code->() } // "ERROR|$@"; close $wr; POSIX::_exit(0) }
close $wr;
chomp(my $got = <$rd> // '');
waitpid $pid, 0;
is $got, $expect, $name; # a child killed by SIGSEGV reports nothing at all
}
```
Cover every way of losing a variable, not just the one that reads naturally:
out of scope, `undef $x`, and `$x = something_else` fail differently.
**Leaks.** `Test::LeakTrace`'s `no_leaks_ok { ⦠}` runs the block twice and compares
the SV count. It sees Perl-level leaks; C-level ones (a `malloc` never freed behind
a still-collected SV) need valgrind:
```bash
PERL_DESTRUCT_LEVEL=2 valgrind --leak-check=full perl -Mblib t/03-whatever.t
```
`PERL_DESTRUCT_LEVEL=2` makes Perl free its own arenas at exit, so what valgrind
still reports is yours.
**Blocking.** A C call that hangs turns a failing test into a hanging suite. Guard
any call that can block on the network with `alarm`, so the failure mode is a
reported failure rather than a CI job that runs until the timeout.
For debugging symbols, rebuild with `perl Makefile.PL OPTIMIZE='-g -O0'`.
( run in 1.130 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )