Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
are constructs that encase statements, rather than statements themselves, so they don't rate the semicolon treatment. For help in spotting these syntax errors and others, try running your code through a beautifier. You can learn about and download the standard Perl beautifier from http://perltidy.sourceforge.net. 7 As a final note, Perl, unlike some of its relatives, doesn't permit the omission of the curly braces in cases where only a single statement is associated with a condition: if ( condition ) statement; # WRONG! # {}s are mandatory in Perl if ( condition ) { statement; } So get used to typing those curly braces--without terminating semicolons! Having just discussed an important flow-control structure that's highly con- ventional--which is an unusual occurrence in a Perl book--we will regain our Perlistic footing by looking next at some valuable yet unconventional operators for string manipulation. 8.4 W RANGLING STRINGS WITH CONCATENATION AND REPETITION OPERATORS Table 8.5 shows some handy operators for strings that we haven't discussed yet. The concatenation operator joins together the strings on its left and right. It comes in handy when you need to assemble a longer string from shorter ones, or easily reorder the components of a string (as you'll see shortly). The repetition operator duplicates the specified string the indicated number of times. It can save you a lot of work when, for example, you want to generate a row of dashes across the screen--without typing every one of them. The concatenation operator doesn't get much use in Minimal Perl, for two rea- sons. First, our routine use of the l option eliminates the most common need for it Table 8.5 Name Concatenation operator String operators for concatenation and repetition Symbol Example . $ab='A' . 'B'; $abc=$ab . 'C'; $abc='A'; $abc.='B'; $abc.='C'; Repetition operator x $dashes='-' x 4; $spaces=' ' x 2; Result AB ABC A AB ABC ---- Explanation The concatenation operator joins together (concatenates) the strings on its left and right sides. When used in its compound form with the assignment operator (.=), it causes the string on the right to be appended to the one on the left. The repetition operator causes the string on its left to be repeated the number of times indicated on its right. 7 To learn about the first Perl beautifier, see http://TeachMePerl.com/perl_beautifier.html. W RANGLING STRINGS WITH CONCATENATION AND REPETITION OPERATORS 265