When should you prefer say vs print, and when should you avoid it?

In Perl, both say and print are used for outputting text to the console, but there are specific situations where you might prefer one over the other.

When to Prefer say

Use say when you want to automatically append a newline character at the end of your output. This is particularly helpful when printing multiple lines, as it simplifies the code.

When to Prefer print

print is preferable when you need more control over the formatting of your output. Unlike say, it does not automatically append a newline, which allows you to format the output more precisely.

When to Avoid say and print

Avoid using say in contexts where you might not want a newline, or if you are not certain that the say feature is supported (such as in older versions of Perl). Similarly, avoid using print if you want straightforward newline handling with less code.

Example

# Using say use feature 'say'; say "Hello, World!"; # Outputs: Hello, World! # Using print print "Hello, "; # Outputs: Hello, print "World!"; # Outputs: World! (no newline automatically)

Perl say print output newline formatting