TEST2::MANUAL::TOOLING::FORMATTER(3pm)
π NAME π
okay so this page is Test2::Manual::Tooling::Formatter β and the manual name up top reads Test2::...ormatter(3pm) from the Perl Programmers Reference Guide. It's literally the guide for writing a custom formatter, in our case a JSONL formatter. π¦
β¨ DESCRIPTION β¨
This tutorial explains a minimal formatter that outputs each event as a json string on its own line. A true formatter will probably be significantly more complicated, but this gives you the basics needed to get started. No cap, it's the starter pack. π
π COMPLETE CODE UP FRONT π
Here's the whole snack, code first:
package Test2::Formatter::MyFormatter;
use strict;
use warnings;
use JSON::MaybeXS qw/encode_json/;
use base qw/Test2::Formatter/;
sub new { bless {}, shift }
sub encoding {};
sub write {
my ($self, $e, $num, $f) = @_;
$f ||= $e->facet_data;
print encode_json($f), "\n";
}
1;
πΈ LINE BY LINE πΈ
use base qw/Test2::Formatter/;β All formatters should inherit fromTest2::Formatter. That's the family tree, bestie.sub new { bless {}, shift }β Formatters need to be instantiable objects, and this is a minimalnew()method. Iconic little constructor behavior.sub encoding {};β For this example we leave this sub empty. In general you should implement this sub to make sure you honor situations where the encoding is set.Test2::V0itself will try to set the encoding toUTF8. πsub write { ... }β Thewrite()method is the most important one: each event is sent here. Main character energy, fr fr.my ($self, $e, $num, $f) = @_;β Thewrite()method receives 3 or 4 arguments, and the fourth is optional.$selfβ The formatter itself.$eβ The event being written.$numβ The most recent assertion number. If the event being processed is an assertion then this will have been bumped by 1 since the last call towrite. For non assertions this number is set to the most recent assertion.$fβ This MAY be a hashref containing all the facet data from the event. More often than not this will be undefined. This is only set if the facet data was needed by the hub, and it usually is not.
$f ||= $e->facet_data;β We want to dump the event facet data. This will set$fto the facet data unless we already have the facet data. Convenient, lowkey.print encode_json($f), "\n";β This line prints the JSON encoded facet data, and a newline. Slay. β¨
π¦ SEE ALSO π¦
Test2::Manualβ Primary index of the manual.
π SOURCE π
The source code repository for Test2-Manual can be found at:
https://github.com/Test-More/test-more/
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
π AUTHORS π
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See:
http://dev.perl.org/licenses/
That's the whole tutorial, bestie. This page is served by perl v5.42.3, dated 2026-11-03, and the manual name is Test2::...ormatter(3pm). πβ¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms