Perl Part I · Module 1 · Introduction to Perl
print and say: Making a Script Speak
You have used print since Chapter 1.1 because a script that says nothing cannot teach you anything. This chapter looks at it properly. print takes any number of things to display, joins them with nothing in between, and adds nothing to the end — which is why the newline is your responsibility and why forgetting it produces output that runs together. Perl also has say, which adds the newline for you, with one catch: it does not exist until you ask for it, and asking is a compile-time decision.
Foundation11 min readPerlPart IFundamentalsOutputprint
Part I · Module 1 · Chapter 1.6 · print and say: Making a Script Speak
1. Why Output Is the First Tool You Learn
A script that computes the right answer and says nothing is indistinguishable from a script that did nothing at all — a point Chapter 1.3 made about silence, and Chapter 1.5 turned into a diagnostic: the last thing printed tells you how far a program got.
That makes print more than a convenience. It is the instrument you use to see inside a running program, and for the whole of Part I it is the only one you have. Worth knowing exactly what it does.
2. print Writes Exactly What You Give It
use strict;
use warnings;
print "check starting\n";check startingThree things are true of print, and all three catch beginners:
- It adds nothing to the end. No newline, no space, no separator. What you supply is what appears.
- It takes any number of arguments, separated by commas, and writes them one after another with nothing in between.
- It does not report success. There is no "printed 1 line" acknowledgement; the output is the only evidence.
3. The Missing Newline
Because print adds nothing, two print statements without newlines produce one run-together line:
use strict;
use warnings;
print "hello";
print "world\n";helloworldNothing is broken here. Perl did precisely what was asked: wrote hello, then wrote world and a newline. The \n is a character you are inserting, not a behaviour you are switching on.
This is the single most common surprise in a beginner's first hour, and it is worth fixing your mental model rather than memorising a rule: \n is part of your data. Once you see it that way, it stops being something to remember and becomes something you are obviously responsible for.
4. Several Things at Once
print accepts a comma-separated list and writes the items with nothing between them:
use strict;
use warnings;
print "a", "b", 1 + 2, "\n";ab3Two observations, both important:
- No separator is inserted.
"a", "b"becomesab, nota b. If you want a space, write one. - An item not in quotes is worked out first.
1 + 2is arithmetic, so3appears. This is the quoted-versus-unquoted distinction from Chapter 1.1, and Chapter 2.5 returns to it in full.
In practice this is how a line of output gets assembled from fixed text and computed values — which is most of what a reporting script does.
5. say: the Newline, Done For You
Perl has a second output instruction that appends a newline:
use strict;
use warnings;
use feature "say";
say "check starting";check startingsay "x" is equivalent to print "x\n". For a script that prints many whole lines, it removes a lot of repeated \n and the occasional forgotten one.
The catch is that say is not available by default. Leave out the feature line and the program does not run at all:
String found where operator expected at say-broken.pl line 5, near "say "check starting""
(Do you need to predeclare say?)
syntax error at say-broken.pl line 5, near "say "check starting""
Execution of say-broken.pl aborted due to compilation errors.Note the class of that failure: aborted due to compilation errors. Per Chapter 1.5, that means nothing in the file ran — not one line of output, even from print statements above the say. Perl also offers a guess in brackets, which is as close as it gets to being helpful: it saw a word it does not know and assumed you meant to define it.
There are two usual ways to switch it on:
use feature "say"; # just this one feature
use v5.10; # this and everything else from Perl 5.10 onwardBoth were verified to work. use v5.10; is common in real code because it enables a group of features at once and documents the minimum Perl the script expects.
6. Mistakes and How They Look
Output all on one line. You forgot \n, as in §3. The giveaway is that the text is correct and only the layout is wrong.
A stray newline where you did not want one. You used say while building a line in pieces. say is for complete lines; use print when the line is not finished yet.
say not recognised. Either the feature line is missing, or it is below the first say. Enabling a feature is a compile-time instruction and applies from where it appears onward, so put it at the top with your other use lines.
Items run together unexpectedly. print "a", "b" gives ab. Perl inserts nothing; if you expected a separator you have to write it.
7. Why This Matters in Engineering Work
Nearly everything Part II produces is text that another program reads: one line per test, one line per timing path, a comma-separated row per measurement. For those consumers, layout is not cosmetic — a missing newline joins two records into one and quietly corrupts the file.
That is the real reason to be precise about who supplies the newline. A human reading output notices a run-together line immediately. A script reading that same output does not: it sees one record where there were two, and reports a number that is wrong in a way nobody questions. Getting output structure right at this stage is what makes the parsing work in Part II possible at all.
8. What You Should Be Able to Say Now
printwrites its arguments with nothing between them and nothing after them.- The newline is data you supply as
\n;printnever adds one. printaccepts several comma-separated items, and an unquoted item is worked out before being written.sayappends a newline but must be enabled withuse feature "say"oruse v5.10.- Using
saywithout enabling it is a compile error, so the program produces no output at all.
9. Exercise
Exercise 1 — predict the layout. Without running it, write down exactly what this produces, including line breaks: print "a"; print "b\n"; print "c", "d", "\n"; Then run it and compare character for character.
Exercise 2 — one line, three pieces. Using a single print with commas, produce the line test: alu_smoke where the word alu_smoke is a separate argument from test: . Confirm there is exactly one space, and that you put it there.
Exercise 3 — break it deliberately. Write a script that prints one, then uses say "two" without any feature line. Predict whether one appears, then run it. Explain the result using the two stages from Chapter 1.5.
Exercise 4 — convert. Take a script with four print "...\n"; statements and rewrite it using say. State one advantage and one risk of the change.
10. Summary
printadds nothing — not a separator between arguments, not a newline at the end. The\nis yours to supply.- Several arguments are written in order with nothing between them, and an unquoted argument is evaluated first.
sayappends the newline and requiresuse feature "say"oruse v5.10, placed above its first use.- A missing feature line is a compile error, so the whole program produces nothing — a direct application of Chapter 1.5's model.
- Output layout is data, not decoration, because in Part II another program will be reading it.
You can now make a script say precisely what you intend. Chapter 1.7 asks the next question: output goes somewhere, and a script has more than one destination — which is how results and warnings stay separable.
Where this fits
Part of the Perl curriculum.
