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

print() and say() in Perl

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.

1. The print() Function

print() 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).

2. The say() Function

Introduced 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.

3. Enabling 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!";

4. Differences and When to Use Which

  • 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.

5. Examples

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";

Conclusion:

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.

  1. Difference between print() and say() in Perl:

    • Description: Both functions are used for printing output, but say() automatically appends a newline, while print() does not.
    • Code Example:
      print "Hello, ";
      print "World!\n";
      
      say "Hello, World!";
      
  2. Using print() function in Perl examples:

    • Description: Printing output using the print() function.
    • Code Example:
      my $name = "Perl";
      print "Hello, $name!\n";
      
  3. Perl say() function vs print() function:

    • Description: Comparing say() and print() regarding newline handling.
    • Code Example:
      print "This is a line without newline";
      say "This is a line with newline";
      
  4. When to use say() instead of print() in Perl:

    • Description: Use say() when automatic newline appending is desired for clearer code.
    • Code Example:
      say "This is a line with newline";
      
  5. How to format output with print() in Perl:

    • Description: Using print() with formatting options.
    • Code Example:
      my $number = 42;
      printf "The answer is: %d\n", $number;
      
  6. Printing variables with print() in Perl:

    • Description: Printing variables using the print() function.
    • Code Example:
      my $language = "Perl";
      print "I love $language programming!\n";
      
  7. Benefits of say() over print() in Perl:

    • Description: say() simplifies code by automatically adding a newline, reducing the chance of missing newlines.
    • Code Example:
      say "This is a line with newline";
      
  8. Examples of say() function in Perl:

    • Description: Additional examples showcasing the say() function.
    • Code Example:
      say "Example 1";
      say "Example 2";
      say "Example 3";
      
  9. Printing to STDERR with print() and say() in Perl:

    • Description: Using print() and say() to print to STDERR.
    • Code Example:
      print STDERR "This is an error message using print\n";
      say STDERR "This is an error message using say";
      
  10. Perl say() newline behavior compared to print():

    • Description: say() appends a newline automatically, making it convenient for line-based output.
    • Code Example:
      my $message = "Hello";
      print $message;    # No newline
      say $message;      # Automatically appends newline