You need to defer evaluation of the description, one solution that comes to mind uses a callback for it. The idea is to return the closure from _construct_colored_description and run it in the subtest function:
my $passed = 0; sub subtest { my ($desc_cb, $test_cb) = @_; $test_cb->(); print $desc_cb->(),"\n"; } sub _construct_colored_description { my $desc = shift; return sub { return $passed ? '[green]' : '[red]', $desc }; }
gives:
[green]subtest description [red]subtest description2
source share