Why You Should Be Using Perl’s Built‑In ‘say’ Function (and When to Avoid It)
Perl’s built‑in ‘say’ function automatically appends a newline, simplifying output and reducing bugs. Learn how to enable it, use it in real scripts, and understand its trade‑offs and limitations.
01 Aug 2025, 20:47 UTC

The Problem: Newlines the Hard Way
When writing Perl scripts, you almost always need to output text that ends with a newline. The classic pattern is:
print "Hello, world!\n";
If you forget the "\n", the output can be garbled or the terminal can behave oddly. Mixing print and say in the same file also creates a visual inconsistency that can confuse readers of your code.
Thesis: ‘say’ Simplifies Output and Reduces Bugs
The core say function, introduced in Perl 5.10, automatically appends a newline to the string you pass, so you no longer have to remember to add "\n" manually. It’s a small change that makes scripts cleaner, easier to read, and less error‑prone.
How to Enable and Use ‘say’
- In Perl 5.10 and newer,
sayis available by default. For older codebases, enable it withuse feature qw(say);oruse 5.10;. - It behaves like
printbut always appends a newline. - Example:
The output will be:use 5.10; say "First line"; print "Second line without a newline"; say "Third line";First line Second line without a newlineThird line
Concrete Example: Listing Files with ‘say’
Below is a minimal script that lists all files in a directory. Notice how say removes the need for explicit newlines.
use 5.10;
use strict;
use warnings;
my $dir = ".";
opendir(my $dh, $dir) or die "Cannot open $dir: $!";
while (my $file = readdir $dh) {
next if $file =~ /^\./; # skip hidden files
say $file; # automatic newline
}
closedir $dh;
Running this script will print each filename on its own line, exactly as you’d expect.
Trade‑Offs and Limitations
- No Custom Separator:
sayalways uses a newline. If you need a different separator (e.g., a comma for CSV output), you must fall back toprintand set$/, $OUTPUT_AUTOFLUSH,or usejoin. - Version Compatibility:
sayis not available in Perl 5.8 or older. Code that must run on those versions should either avoidsayor provide a compatibility shim. - Semantics with
printvssay: Mixing the two can lead to confusing output if you forget thatsayadds a newline automatically.
Practical Checklist
- Run
perl -vto confirm your interpreter is ≥5.10. - Add
use 5.10;at the top of your scripts. - Replace any
print "…\n";withsay "…";. - For custom separators, keep
printand explicitly set$/or usejoin. - Run your script and verify that each output line ends with a newline by piping to
od -cor inspecting the terminal.
Conclusion
Adopting say in modern Perl codebases is a quick win: it cleans up your print statements, eliminates a common source of bugs, and makes your intent clearer. Just remember its limitations—especially the lack of a custom separator and the version requirement—so you can decide whether it fits your project’s constraints.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.