Say It Simply: Using Perl’s Built‑In ‘say’ to Cut Boilerplate and Avoid Newline Pitfalls
Perl’s ‘say’ keyword automatically appends a newline, letting you drop the ‘\n’ from every print. Learn how to enable it, refactor existing code, and understand when the automatic newline can bite you. Try it now with a quick command‑line test or a small script.
22 Aug 2025, 21:27 UTC

Problem: The Never‑Ending Print Boilerplate
When writing Perl scripts that generate human‑readable text, you almost always end up with the same pattern:
print "Hello, world!\n";
Every line you want to output ends with a literal \n or you have to remember to add a separate print "\n"; after a block of prints. This tiny repetition is harmless but it clutters code, makes refactoring tedious, and can lead to subtle bugs when you forget the newline or accidentally double‑print it.
Thesis: The Built‑In say Keyword Cuts the Boilerplate
Perl introduced the say keyword in version 5.10 as part of the feature pragma. It behaves like print but automatically appends a newline to the output. That means you can write:
use feature 'say';
say "Hello, world!";
or, on the command line, simply add -Mfeature=say:
perl -Mfeature=say -e 'say "Hello, world!";'
The result is the same as the longer print version, but the code is cleaner and less error‑prone.
Enabling say – Two Quick Paths
- At the top of a script:
use feature 'say';– this activatessayfor the entire file. - Command‑line switch:
-Mfeature=say– useful for one‑liners or when you cannot modify the source.
Both methods require a Perl interpreter that reports version 5.10 or newer. You can verify the version with:
perl -v
Look for a line that starts with This is perl followed by a number ≥ 5.10.
Practical Example: Refactor a Small Script
Suppose you have the following script that prints a CSV header and a few rows:
#!/usr/bin/perl
use strict;
use warnings;
print "Name,Age,Location\n";
print "Alice,30,New York\n";
print "Bob,25,Los Angeles\n";
Refactoring it with say is almost a one‑liner change. Replace the three print statements with say and add the pragma at the top:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
say "Name,Age,Location";
say "Alice,30,New York";
say "Bob,25,Los Angeles";
Running perl csv.pl yields the same output, but the code now reads naturally and the risk of missing a \n is eliminated.
When the Automatic Newline Can Bite You
Because say always appends a newline, it is ideal for plain text logs but problematic in contexts where you need fine‑grained control:
- Binary output – a newline is a byte and will corrupt the data stream.
- CSV or TSV files that require a single header line – using
sayfor the header andprintfor rows keeps the file well‑formed. - Multi‑line string building – if you concatenate strings manually, the extra newline can introduce unwanted blank lines.
In such cases, stick with print or use say only where the newline is acceptable.
Trade‑Offs and Compatibility
| Aspect | Benefit | Risk / Limitation |
|---|---|---|
| Readability | Cleaner syntax for simple output | May hide the newline if you’re not careful |
| Portability | Works on all Perl 5.10+ installations | Pre‑5.10 Perl will throw a syntax error unless the feature is enabled or the code is updated |
| Binary safety | Not suitable for binary data streams | Using say in such contexts will corrupt the output |
| Performance | Negligible difference from print | None |
Actionable Next Steps
- Check your Perl version:
perl -v. If it’s 5.10 or newer, you’re ready. - Try the one‑liner:
perl -Mfeature=say -e 'say "Hello, world!";'. You should see a newline after the text. - Open an existing script and add
use feature 'say';at the top. Replace anyprint "...\n";withsay "...";and run the script to confirm the output is unchanged. - When refactoring, keep an eye out for places where the automatic newline would be undesirable (e.g., CSV headers or binary dumps) and use
printthere. - Document the change in your codebase or README so future contributors know that
sayis in use and the version requirement.
By adopting say where appropriate, you reduce boilerplate, improve readability, and keep your scripts clean. Just remember the newline it adds and the version requirement, and you’ll avoid the most common pitfalls.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.