Portrait of Michael Limberger

Michael Limberger

Need me? Email mike@limberger.ca

Perl

Eval loop

The earlier, shorter countdown

2024-06-24

This note is about Perl's eval loop pattern. It is one of those small tricks that looks odd the first time you see it, then becomes hard to put down once you need dynamic code paths.

#!/usr/bin/env perl
use feature qw|say|;

$_ = <<'EOF';
10;
s~(\\d+)(?{ say qq($1) })~$1-1~e;
sleep 1;
$1 ? eval : say q(Countdown complete!);
EOF

eval;

Match a number, print it, replace it with itself minus one, sleep, and if $1 is still true, eval the string again.

Swiss Army chainsaw, not a template for work code. The longer walkthrough is the 2025 piece.