Going Deeper

Perl is such a wide and deep language that I can't hope to explain it all without ending up with a phonebook-sized volume. In this section, then, are the things you can do with scalar data that I haven't covered in the previous sections. Some of these things we'll explore in more detail later on in this book, but for the most part these topics are parts of the language you'll have to explore on your own.

As I mentioned yesterday, throughout this book, when I refer to the perlop or perlfunc man pages—or to any Perl man pages—you can find these pages as part of the documentation for your Perl interpreter, or on the Web at http://www.perldoc.com/.

Quoting Strings and Patterns

The quote characters '' and "" in Perl might seem like immutable requirements for creating strings, but, actually, they're not. There are a number of ways you can create strings in Perl besides using single and double quotes, some of which interpolate variables, and some of which don't. For example, instead of creating the string 'Beware the ides of March' with single quotes, you could use the q// operator, like this:

q/Beware the ides of March/

Don't like quotes or slashes? No problem. You can use the q// operator with any non-alphanumeric or nonwhitespace character (that is, no letters, numbers, spaces, or tabs), as long as you use the same character to begin and end the string and start the whole thing with a q. The following examples are all equivalent in Perl:

'Beware the ides of March'

q/Beware the ides of March/

q#Beware the ides of March#

q(Beware the ides of March)

As with single quotes, the q// operator does not interpolate variables. For double quotes and variable interpolation, you can use the qq// operator in the same way:

"stored $num reminders"
qq/stored $num reminders/
qq^stores $num reminders^

You might want to use these formats if, for example, you have a string with lots of quotes in it that all need escaping. By substituting, say, a slash character for the quote, you can put quotes inside your strings without having to escape them.

See the perlop man page for details on the various quote-like operators.

If you're thinking q// and qq// look something like m//, you're right; they're both considered quoting operators: one for strings, the other for patterns. In fact, you can use m// in the same way, with any nonalphanumeric character substituted for the slash:

m?foo?

m$cookie$

If you do use a pattern with a character other than the slash, note that you do have to retain the m at the start of the pattern. If you leave it off you must use the slash.

Unquoted Strings (Barewords)

Single words in lowercase without quotes that have no other meaning in Perl are interpreted as strings. Perl calls them barewords, and they're generally discouraged because they make scripts very hard to read, are error-prone, and you never know when a bareword you use a lot will end up being a reserved word in a future version of the language. Avoid them. If you have Perl warnings turned on, it will complain about this sort of thing.

Upper and Lowercase Escapes

In Table 2.1, I summarized the escape characters available for interpolated (double-quoted) strings. Amongst those were escapes for upper and lowercasing strings (l, u, L, U, and E). Use these escapes to force upper or lowercase values in strings.

The l and u escapes are used to make the next letter in the string lower or uppercase. You could use u, for example, to make sure the start of a sentence was capitalized. L and U are used to make lower or uppercase a series of characters inside a string; they will both convert the characters that follow in the string until the end of the string or until a E occurs. These escapes aren't necessarily useful in ordinary quoted strings, but they'll become useful later on when you create patterns for search-and-replace procedures.

More About Variable Interpolation in Strings

Variable interpolation is the capability of Perl to replace variable references in strings with their actual values. In some cases, however, it might be difficult for Perl to figure out where the variable ends. For example, take the following string:

"now reading the $valth value
";

In this string, the variable to be interpolated is actually called $val and contains a number. But because the final string will say something like "now reading 12th value", the name of the variable butts up against the th part and Perl looks for a variable called $valth instead of $val. There are a number of ways around this sort of problem—not the least of which is concatenating multiple strings to form the final one. But, Perl also provides syntax to get around this problem, like this:

"now reading the ${val} th value
";

The curly braces ({}) in this case are simply delimiters so that Perl can figure out where the variable name starts and ends; they are not printed in the final value. Using curly braces in this way can help get around problems in variable interpolation.

Octal and Hexadecimal Numbers

The 0123 and 0xabc formats you learned about in the section on numbers apply only to the numbers you actually type into the code for your scripts (literals in Computer Science parlance). Input from strings or from the keyboard in octal or hex notation will remain as strings; you can convert those strings to actual numbers using the oct function like this:

$num = '0x432';
print $num;      # prints 0x432
$hexnum = oct $num;
print $hexnum;   # prints 1074 (decimal equivalent)
$num = '0123';
print (oct $num);  # prints 83

The oct function can tell from the context whether the string is an octal or hex number; you can also use the hex function for hex numbers only.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset