Perl printing examples

Perl printing FAQ: Can you share some Perl printing examples?

There are several different ways to print in Perl, and I thought I'd share some examples here today.

The Perl print function

Generally you'll print simple output with the Perl print function. As a simple example, you can print a string literal using the Perl print function, like this:

print "Hello, world.\n";

Notice that you need to supply the newline character at the end of your string. If you don't supply that newline character, and print multiple lines, they'll all end up on one long line of output, like this:

Hello, world.Hello, world.Hello, world.

So, when using the Perl print function, don't forget the newline character at the end of the string you're printing.

Printing Perl variables with print

In any real-world Perl script you'll need to print the value of your Perl variables. To print a variable as part of a a string, just use the Perl printing syntax as shown in this example:

$name = 'Alvin';
print "Hello, world, from $name.\n";

When you run this Perl script, you'll see the following output:

Hello, world, from Alvin.

Perl does the work of replacing the variable $name with the value of that variable, Alvin.

Perl printing - Double and single quotes

One important thing to note in this example is the use of double quotes with that Perl print statement. In Perl, when you place a variable inside of double quotes, Perl can replace the value of the variable name. This is called "variable interpolation", and it works when you use double quotes, but it does not work when you use single quotes.

The Perl print function is essentially blind to anything you place inside single quotes, and will just print whatever you put in between single quotes without giving it a second thought. For example, if you try that same Perl print statement with single quotes, like this:

# this won't work as desired:
print 'Hello, world, from $name.\n';

In this example, Perl can't see inside the single quotes, so it can't replace $name with Alvin, and it ends up printing $name instead, as shown here:

Hello, world, from $name\n

I forgot to mention the \n character, but as you can see in that output, when you use single quotes, the Perl print function also can't see the \n character, so it just prints the two characters \ and n, instead of actually seeing that character and printing a newline character in its place.

Perl printing - Other ways to print Perl variables

I could have also printed that previous example as shown here:

$name = 'Alvin';
print "Hello, world, from " . $name . ".\n";

The result is the same, but I don't care for this Perl printing approach very much. For me, it's much harder to read, especially when you need to print more than one variable at a time.

Formatted printing with the Perl printf function

Finally, you can also print in Perl using the Perl printf function. I've covered the printf function in great detail in my Perl printf formatting tutorial, so I won't get into that again here. Please visit that tutorial for more information.

Other Perl printing ideas

I'm sure there are other Perl printing approaches than what I can think of right now, but I thought I'd put these out here to get started with. If you have any Perl printing examples you'd like to share, just use the comment form below.