- Assignment Operators
- Increment and Decrement Operators
- String Concatenation and Repetition
- Operator Precedence and Associativity
- Using Patterns to Match Digits
- An Example: Simple Statistics
- Input and Output
- Another Example: Stocks
- A Note About Using Functions
- Going Deeper
- Summary
- Q&A
- Workshop
- Answers
Going Deeper
Had enough of strings and numbers yet? Want to learn more? No problem. This section has a number of other features to look at concerning scalar data before we forge on ahead to lists.
Operators are all discussed in the perlop man page, whereas functions are discussed in the perlfunc man page. As I mentioned before, you can get to all these pages through the use of the perldoc command or on the Web at http://www.perl.com/pub/doc/manual/html/pod/.
Useful Number and String Functions
Perl includes quite a few built-in functions for a variety of purposes. Appendix E, "Perl Functions," contains a summary of those functions, and the perlfunc man page also describes them in further detail. In particular, Perl includes a number of useful functions for numbers and strings, including those summarized in Table 3.4. We'll explore some of these in more detail in forthcoming chapters; others you'll have to explore on your own.
Table 3.4 Number and String Functions
Function |
What it does |
abs |
Absolute value |
atan2 |
Arctangent |
chr |
The character represented by a number in the ASCII character set |
cos |
Cosine |
exp |
e to the power of (use ** for exponentiation) |
int |
Truncate decimal part of a float |
index |
Returns the position of the first occurrence of the substring in string |
lc |
Lowercase a string |
lcfirst |
Lowercase the first character in a string |
length |
Length (number of bytes) |
log |
Logarithm |
ord |
The number of a character in the ASCII character set |
rand |
Random number |
reverse |
Reverse a scalar |
rindex |
Reverse index (starts from end of string) |
sin |
Sine |
sqrt |
Square root |
substr |
Returns a substring starting at an offset of some length |
uc |
Uppercase a string |
ucfirst |
Uppercase the first letter in a string |
Bitwise Operators
Perl provides the usual set of C-like operators for twiddling bits in integers: ~, <<, >>, &, |, and ^, as well as assignment shortcuts for those operators. See the perlop man page for specifics.
The cmp and <=> Operators
In addition to the relational operators I described in the section on comparisons, Perl also has the <=> and cmp operators. The former is for numbers, and the latter for strings. Both return -1, 0, or 1 depending if the left operator is greater than the right, the operators are equal, or if the right operator is greater than the left, respectively. These operators are most commonly used for creating sort routines, which you'll learn more about on Day 8.
Functions and Function-Like Operators
Perl's built-in functions actually fall into two groups: functions that are functions, and operators that take one argument and masquerade as functions. The function-like operators fall in the middle of the precedence hierarchy and behave like operators in this respect (whereas function calls with parentheses always have the highest precedence). See the perlop man page under "Named Unary Operators" for a list of these functions.