← Back to Perl Course | Chapter 1: Introduction | Lesson 6 of 7

print and say

print writes text exactly as given, and say does the same but adds a newline.

In this page:

  1. print and say

print and say

print outputs a list of values with no separator and no automatic newline. The say function, enabled with use feature say or use v5.10, appends a newline for you. Both can print to a filehandle such as STDERR when you put the handle first, with no comma after it.

Note: Set $, to change the separator between print items and $\ to add something at the end of each print.

Example: print and say

perl
use strict;
use warnings;
use feature 'say';

print "one", "two", "three";
print "\n";
say "hello";
say "a", "b", "c";
{
    local $, = "-";
    say "x", "y", "z";
}
print STDOUT "to STDOUT\n";

# Output:
# onetwothree
# hello
# abc
# x-y-z
# to STDOUT
Common Mistakes
  1. Forgetting that print adds no newline
  2. Calling say without enabling the feature
  3. Putting a comma after the filehandle in print STDERR, "text"
Chapter Summary
  • print adds no newline
  • say adds a newline
  • say needs use feature say
  • No comma after a filehandle
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.