Amakuru.net

A CPAN module written entirely by Claude — and the test suite that proves it

An iterative binary search in Perl with comparator support, context-sensitive returns, and 150 tests across seven files. Generated end to end as a controlled experiment.

I still write a fair amount of Perl — not new Perl in the starting-a-project sense, but enough maintenance work to recognise good Perl when I see it. The question I wanted to answer with this small experiment was whether a current model could produce CPAN-quality Perl: not just code that compiles and runs, but code that’s idiomatic and structurally sound enough to belong on CPAN. Algorithm::BinarySearchFoobar is the result — a small module implementing iterative binary search with comparator support, generated end to end by Claude. I wrote the brief and ran the reviews; everything in the repo is the model’s output.

A tight brief

The brief was tight on purpose, because correctness is what makes the experiment legible:

  • Iterative, O(log n) time, O(1) space, with an overflow-safe midpoint.
  • Custom comparators, like sort — a coderef like sub { $_[0] cmp $_[1] }.
  • Context-sensitive return: scalar context gives index or undef; list context gives ($index, $found) with proper insertion-point semantics.
  • Input validation via croak, not die — Perl module convention.
  • Exporter support: nothing exported by default, binary_search available via @EXPORT_OK.

For a textbook algorithm, the correctness bar is well-defined; the experiment either clears it or doesn’t.

The test suite

What carried the module past “demo” was the 150 tests across seven files: basic cases, comparator variants, context-sensitivity, edge cases (empty / undef / duplicates / negatives / floats), error paths, large arrays (10M elements, sparse, duplicates, power-of-two boundaries), and Exporter behaviour.

The large-array file is where the algorithm earns its keep — power-of-two boundary tests catch the classic mid-point overflow bug. A “splice-verify-sorted” property test (remove the element binary search returned, splice it back at the reported insertion point, assert the list is still sorted) appeared without prompting and is the kind of test a careful human reviewer would have asked for.

Was it worth it?

For textbook algorithms with strict constraints, current models can write CPAN-quality code. Whether they should write business logic is a different question, because business logic doesn’t have a textbook bar to clear. The narrow result of this experiment is that “it can’t write real code” doesn’t hold up for the kind of code with a well-defined correctness contract.

dmorel69/nonrecursive-binarysearch — Perl, CPAN-style