© Moritz Lenz 2017

Moritz Lenz, Perl 6 Fundamentals , https://doi.org/10.1007/978-1-4842-2899-9_5

5. Testing say()

Moritz Lenz

(1)Fürth, Bayern, Germany

In the previous chapter, I changed some code so that it wouldn’t produce output, and instead did the output in the MAIN sub, which conveniently went untested.

Changing code to make it easier to test is a legitimate practice. But if you do have to test code that produces output by calling say, there’s a small trick you can use: say works on a file handle, and you can swap out the default file handle, which is connected to standard output. Instead of the default, you can put a dummy file handle in its place that captures the lower-level commands issued to it, and record this for testing.

There’s a ...

Get Perl 6 Fundamentals : A Primer with Examples, Projects, and Case Studies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.