LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1216 min read

Input & Output

Learn Perl input and output: print versus say, reading user input from STDIN, and cleaning up trailing newlines with chomp.

Introduction

So far, every script in this course has produced fixed output without ever reacting to anything typed by a user. Real-world scripts, especially the sysadmin-style tools Perl is famous for, often need to read input — from a person typing at a terminal, or from data piped in from another program. This lesson covers exactly that.

What You Will Learn
  • A deeper look at print, plus the more convenient say function.
  • How to read a line of input from STDIN.
  • Why chomp is essential when working with input.
  • How to build a small, complete interactive script.

As covered earlier in this course, print writes its arguments to standard output with no automatic separator or trailing newline, so you must include \n yourself whenever you want a line break.

use strict;
use warnings;
print "This line ";
print "continues here because there is no newline yet.\n";
print "This is a fresh line.\n";
Program Output

Click Run to see what this code prints.

say: print With an Automatic Newline

Modern Perl (5.10 and later) offers say, which behaves exactly like print but automatically appends a newline for you. Because it is a newer feature, it must be explicitly enabled with use feature 'say'; (or, in current Perl, simply by declaring a modern enough language version).

use strict;
use warnings;
use feature 'say';
say "This line automatically ends with a newline.";
say "So does this one — no need for \\n.";
Program Output

Click Run to see what this code prints.

Which Should You Use?

say is generally more convenient for straightforward line-by-line output, while print remains useful when you want precise control over exactly where newlines go, such as building up a single line from multiple pieces.

Reading From STDIN

STDIN is Perl's built-in filehandle representing standard input — typically whatever the user types at the terminal, or data piped in from another command. Wrapping it in angle brackets, <STDIN>, reads a single line of input, including its trailing newline character.

use strict;
use warnings;
print "What is your name? ";
my $name = <STDIN>;
print "Hello there, $name";
Example Session

Click Run to see what this code prints.

Notice the extra blank line after "Hello there, Grace" in the output above. That happens because <STDIN> captures the newline the user produced by pressing Enter, and print then adds it again along with the string — this is exactly the problem chomp solves next.

chomp: Removing Trailing Newlines

The chomp function removes a single trailing newline character from the end of a string, if one is present. It is used constantly alongside <STDIN>, since input read this way almost always carries an unwanted newline from the user pressing Enter.

use strict;
use warnings;
print "What is your name? ";
my $name = <STDIN>;
chomp($name); # removes the trailing newline from the input
print "Hello there, $name!\n";
Example Session

Click Run to see what this code prints.

With chomp applied, the output is now clean, with the exclamation mark landing right after the name and no stray blank line.

chomp Modifies in Place

chomp($name) changes $name directly rather than returning a new, cleaned-up string. Writing my $clean = chomp($name); would be a mistake, since chomp actually returns the number of characters removed, not the cleaned string.

A Complete Interactive Example

Let's combine everything from this lesson into one small, complete, interactive script that asks for two pieces of information and produces a formatted summary.

use strict;
use warnings;
use feature 'say';
print "Enter your favorite programming language: ";
my $language = <STDIN>;
chomp($language);
print "Enter how many years you've used it: ";
my $years = <STDIN>;
chomp($years);
say "-" x 30;
say "Language: $language";
say "Experience: $years year(s)";
say "-" x 30;
Example Session

Click Run to see what this code prints.

This script reuses ideas from across this entire course: use strict/use warnings, string interpolation, the x repetition operator, and now STDIN with chomp — a good sign that these fundamentals are starting to click together.

Common Mistakes

Avoid These Mistakes
  • Forgetting to chomp input read from <STDIN>, leaving a stray newline that breaks string comparisons and formatting.
  • Assuming chomp returns the cleaned string — it modifies its argument in place and returns the count of characters removed instead.
  • Forgetting use feature 'say'; (on older Perl versions) before using say.
  • Not printing a prompt before reading input, leaving the user staring at a blank terminal with no idea what to type.

Best Practices

  • Always chomp input immediately after reading it from <STDIN>, before using it further.
  • Print a clear prompt before every <STDIN> read so the user knows what is expected.
  • Prefer say for simple line-based output once your Perl version supports it, to reduce repetitive \n clutter.
  • For anything beyond simple scripts, validate input after chomping it, since users can type anything.

Frequently Asked Questions

It returns one line of input as a string, including the trailing newline character produced when the user presses Enter. That is why chomp is almost always used immediately afterward.

Yes — reading <STDIN> in list context (for example, my @lines = <STDIN>;) reads all available lines into an array, one element per line, until end-of-input is reached.

On sufficiently recent Perl versions you can enable modern features more broadly with a version declaration, but explicitly writing use feature 'say'; is the clearest, most portable way to guarantee say is available.

Key Takeaways

  • print requires a manual \n for line breaks; say adds one automatically once enabled.
  • <STDIN> reads a single line of standard input, including its trailing newline.
  • chomp removes a trailing newline from a string in place and is essential after reading from STDIN.
  • Always prompt the user with print before reading input, so they know what to type.

Summary

You have now completed the foundational tour of Perl: its history, installation, core data types, context, operators, and interactive input/output. With these fundamentals in place, you are ready to start controlling program flow itself, starting with conditional statements in the next lesson.

Next Lesson →

Conditional Statements