Tinkering with Perl

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  29  30  31  32  33  34  35  36  37  38  Next  Skip Forward
Printer-Friendly Version

Output

The print statement is useful for output. Let me give an example of a very famous program:

#!/usr/local/bin/perl
print "Hello, world!\n";

The first line is a line you should put at the beginning of every Perl program you write. The second line prints, "Hello, world!" It is the second line we are studying.

What's that funny "\n"? What does it mean? Well, Perl doesn't know by itself when a line should end. So, we put "\n" at the end of each line, to tell it to go to the next line.

Here is another example of some code using print:

print "The average is " . $Average . ".\n";

Now, what does that all mean?

Remember that, earlier, when we talked about scalars, we could concatenate two strings by putting a period ('.') between them. This takes three strings: "The average is ", $Average, and ".\n", and sticks them all together. Let's say that $Average is 4.5. Then the result will look like:

The average is 4.5.

The "." in ".\n" is just a period for the end of the sentence, and "\n" means the end of line.

See also:

Statements - Scalars - Input and output - Input

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  29  30  31  32  33  34  35  36  37  38  Next  Skip Forward
Printer-Friendly Version