![]() Please visit that tutorial for more information. I've covered the printf function in great detail in my Perl printf formatting tutorial, so I won't get into that again here. Formatted printing with the Perl printf functionįinally, you can also print in Perl using the Perl printf function. ![]() For me, it's much harder to read, especially when you need to print more than one variable at a time. ![]() The result is the same, but I don't care for this Perl printing approach very much. I could have also printed that previous example as shown here: Perl printing - Other ways to print Perl variables 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. 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: For example, if you try that same Perl print statement with single quotes, like this: 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. This is called "variable interpolation", and it works when you use double quotes, but it does not work when you use single quotes. In Perl, when you place a variable inside of double quotes, Perl can replace the value of the variable name. One important thing to note in this example is the use of double quotes with that Perl print statement. Perl does the work of replacing the variable $name with the value of that variable, Alvin. When you run this Perl script, you'll see the following output: To print a variable as part of a a string, just use the Perl printing syntax as shown in this example: ![]() In any real-world Perl script you'll need to print the value of your Perl variables. So, when using the Perl print function, don't forget the newline character at the end of the string you're printing. 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: Notice that you need to supply the newline character at the end of your string. As a simple example, you can print a string literal using the Perl print function, like this: Generally you'll print simple output with the Perl print function. There are several different ways to print in Perl, and I thought I'd share some examples here today. Perl printing FAQ: Can you share some Perl printing examples? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |