Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, print()
and say()
are functions used to display information. While they are similar, there are key differences between the two. In this tutorial, we'll explore both of these functions and how to use them effectively.
print()
Functionprint()
is one of the most commonly used functions in Perl to display data. It takes a list of values and sends them to the standard output:
#!/usr/bin/perl use strict; use warnings; print "Hello, ", "world!\n";
Here, print()
displays "Hello, world!" followed by a newline character (\n
).
say()
FunctionIntroduced in Perl 5.10, say()
is similar to print()
, but it automatically appends a newline to the output:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; say "Hello, world!";
Notice that with say()
, you don't need to add the \n
character.
say()
To use say()
, you need to enable it using the feature
pragma, as shown in the above example. Alternatively, you can also enable it with the -E
command-line switch or by including the 5.10.0
version:
use 5.10.0; say "Hello, world!";
Automatic Newline: The main difference between print()
and say()
is the automatic newline at the end with say()
. This makes say()
convenient for quick prints or when displaying multiple lines of data.
Portability: If you're writing a script that needs to run on older Perl versions (pre-5.10), you should stick to print()
. However, for most modern scripts, using say()
can lead to cleaner and more concise code.
Flexibility: print()
is more flexible in that you can decide whether to include a newline or not. This can be handy in situations where you want to print data in the same line successively.
Using print()
:
print "Enter your name: "; my $name = <STDIN>; print "Hello, $name";
Using say()
:
say "Enter your name: "; my $name = <STDIN>; chomp $name; # To remove the newline character at the end of the input say "Hello, $name";
Both print()
and say()
are powerful functions for displaying data in Perl. Your choice between them will often depend on your specific needs and the Perl version you're targeting. For general-purpose printing with modern Perl versions, say()
offers a concise and clean way to display information.
Difference between print() and say() in Perl:
say()
automatically appends a newline, while print()
does not.print "Hello, "; print "World!\n"; say "Hello, World!";
Using print() function in Perl examples:
print()
function.my $name = "Perl"; print "Hello, $name!\n";
Perl say() function vs print() function:
say()
and print()
regarding newline handling.print "This is a line without newline"; say "This is a line with newline";
When to use say() instead of print() in Perl:
say()
when automatic newline appending is desired for clearer code.say "This is a line with newline";
How to format output with print() in Perl:
print()
with formatting options.my $number = 42; printf "The answer is: %d\n", $number;
Printing variables with print() in Perl:
print()
function.my $language = "Perl"; print "I love $language programming!\n";
Benefits of say() over print() in Perl:
say()
simplifies code by automatically adding a newline, reducing the chance of missing newlines.say "This is a line with newline";
Examples of say() function in Perl:
say()
function.say "Example 1"; say "Example 2"; say "Example 3";
Printing to STDERR with print() and say() in Perl:
print()
and say()
to print to STDERR.print STDERR "This is an error message using print\n"; say STDERR "This is an error message using say";
Perl say() newline behavior compared to print():
say()
appends a newline automatically, making it convenient for line-based output.my $message = "Hello"; print $message; # No newline say $message; # Automatically appends newline