Thursday 3 September 2020

LPI: Perl printing examples

LPI Exam Prep, LPI Tutorial and Material, LPI Study Materials, LPI Learning, LPI Guides, LPI Certification

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

Also Read: LPI Certifications


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 = 'LPICentral';
print "Hello, world, from $name.\n";

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

Hello, world, from LPICentral.

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

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 LPICentral, 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 = 'LPICentral';
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.

Related Posts

0 comments:

Post a Comment