10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPENDIX ■ PERL BASICS 285In the last example in the table, 1F18 is a hexadecimal number referring <strong>to</strong> a character inthe Unicode character set, which runs from 0000-FFFF. As another example, \ x{ 2620} is theUnicode character for a skull-and-crossbones!White SpaceAs mentioned previously, white space is the name we give <strong>to</strong> tabs, spaces, and newlines. <strong>Perl</strong> isvery flexible about where you put white space in your program. You’ve already seen that you’refree <strong>to</strong> use indentation <strong>to</strong> help show the structure of blocks. You don’t need <strong>to</strong> use any whitespace at all, if you don’t want <strong>to</strong>. If you’d prefer, your programs can all look like this:print"Top level\ n";{ print"2nd level\ n";{ print"3rd level\ n";}print"Where are we?";}This is considered a bad idea. White space is another <strong>to</strong>ol we have <strong>to</strong> make our programsmore understandable; let’s use it as such.Types of DataA lot of programming jargon is about familiar words in an unfamiliar context. You’ve alreadyseen a string, which was a series of characters. You could also describe that string as a scalarliteral constant. What does that mean?By calling a value a scalar, you’re describing the type of data it contains. If you rememberyour math (and even if you don’t), a scalar is a plain, simple, one-dimensional value. In math,the word is used <strong>to</strong> distinguish it from a vec<strong>to</strong>r, which is expressed as several numbers. Velocity,for example, has a pair of coordinates (speed and direction), and so must be a vec<strong>to</strong>r. In <strong>Perl</strong>,a scalar is the fundamental, basic unit of data of which there are two kinds: numbers andstrings.A literal is value that never changes. The value 5 is a scalar literal—and is literally 5; it cannever be 4. <strong>Perl</strong> has three types of scalar literals: integers (such as 5), floating-point numbers(like 3.14159), and strings (for example, “hello, world”). To put it another way, a literal is a constant—itnever changes, as opposed <strong>to</strong> a variable, which is a piece of memory that can holda scalar value. Variables are so named because the value s<strong>to</strong>red within them can vary. Forinstance, $number can be assigned 5, and then later can be changed <strong>to</strong> the value 6. We will talkmore about variables later in this appendix.NumbersThere are two types of numbers that we’re interested in as <strong>Perl</strong> programmers: integers andfloating-point numbers. The latter we’ll come <strong>to</strong> in a minute, but let’s work a bit with integersright now. Integers are whole numbers with no numbers after the decimal point, such as 42,–1, or 10. The following program prints a couple of integer literals in <strong>Perl</strong>:#!/usr/bin/perl -w# number1.plprint 25, -4;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!