This tutorial uses examples for string manipulations using perl. In
particular, we concentrate on the following four useful constructs:
1. String concatenation using the "." operator
2. Matching for patterns in a string using m/PATTERN/
3. Substituting one expression for another using s/OLD/NEW/
4. Translating one set of characters to another using
tr/OLD-CHARS/NEW-CHARS/
Along the way, we may pick up other neat tricks such as using subroutines,
bindings etc.
#!/usr/local/bin/perl
$Astring = "This is the first part";
print ( $Astring); print ("\n");
$Bstring = "of a long string";
print ( $Bstring); print ("\n");
$LONGstring = $Astring . $Bstring;
print ( $LONGstring); print( "\n");
NOTES:
When you want to join two strings, use the dot operator, ".", as in this
example.
#!/usr/local/bin/perl $Astring = "This is the first part"; print ( $Astring); print ("\n"); $Bstring = "of a long string"; print ( $Bstring); print ("\n"); $LONGstring = $Astring . " " . $Bstring; print ( $LONGstring); print( "\n");
NOTES:
The dot operator can be used multiple times in an expression.
#!/usr/local/bin/perl $_ = "Bus 298 goes to Lam Tin every 20 minutes"; if ( m/Lam Tin/) { print ("Found a bus to Lam Tin !\n"); } else { print ( "No bus to Lam Tin.\n"); } if ( m/Choi Hung/) { print ("Found a bus to Choi Hung.\n"); } else { print ( "No bus to Choi Hung.\n"); }
NOTES:
The matching operator, m/PATTERN/ will return TRUE if PATTERN matches a patern
in a variable called $_. The $_ is a special variable which is used for all
pattern matches for m//, s/// and tr/// operators.
The match operator can use UNIX regular expression syntax modifiers. most
useful are the following:
m/PATTERN/g will do a Global search (find all occurences).
m/PATTERN/i will Ignore the case (lower case, upper case) when matching.
The m// is so commonly used, that perl will do matching even when you don't
write the "m", as in the next example !
#!/usr/local/bin/perl $_ = "UPPER CASE ? lower case ? Mixed CaSe ? No Problem !"; @case_instances = /case/ig; foreach $c (@case_instances) { print ("$c \n"); }
NOTES:
1. The match is performed by the expression: /case/ig
instead of the more formal: m/case/ig
2. The use of "i" ignores the case while matching.
3. The use of "g" causes Global match (every instance); Note also that the
match operator can be used to directly return an array !
4. Some useful matching patterns include matching word characters (any
alphanumeric character, or the underscore) using \w, multiple word characters
(that is, a single word) using \w+, single space character using \s, non-word
characters (punctuation marks) using \W, tabs using \t, newline using \n
etc.
#!/usr/local/bin/perl $_ = "I love Jackie Chan movies! I love kung-fu movies."; print "$_ \n"; s/love/HATE/; print( "$_ \n"); s/(HATE|love)/Admire/g; print( "$_ \n"); s/admire/dislike/ig; print( "$_ \n"); s/dis//g; print( "$_ \n");
NOTES:
This example shows several features of the substitute operator.
1. As for the match operator, the operation is carried out on a variable $_;
2. The s/OLD/NEW/ will replace the FIRST OCCURENCE of "OLD" with "NEW".
3. The virtical bar, "|", is used to denote alternatives. It may be used many
times in the same expression. As for the match operator, you may use "g" and
"i" filters.
4. Notice how "s/dis//g works: Every occurence of pattern "dis" is replaced by
a null string.
#!/usr/local/bin/perl $_ = "aaa AAA bb BB CC cc"; print ("$_ \n"); tr/ab/AB/; print ("$_ \n"); $_ = "i can't hear this."; print ("$_ \n"); tr/i/I/; print ("$_ \n"); tr/a-z/A-Z/; print ("$_ \n");NOTES:
1. tr translates all occurences of character 'OLD' with 'NEW' in tr/OLD/NEW/
2. All substitutions are global (you don't need a /g at the end.
3. You can specify a set of substitutions, as in a-z (that is, letters from "a" to "z"), substituted by A-Z (letters from "A" to "Z") respectively. That is, "a" replaced by "A", "b" by "B", .. etc.
Example7.pl#!/usr/local/bin/perl print ("Enter a 2 letter password:\n"); system ( "stty -echo"); $password = <STDIN>; chop( $password); system( "stty echo"); print (" To turn ON the screen, type your password in rot13\n"); system( "stty -echo"); $badpass = 1; $ntries = 0; while (($badpass != 0) && ($ntries < 2)) { $trial = <STDIN>; chop( $trial); $rotated = do rot_13( $trial); if ($rotated eq $password) { $badpass = 0; system( "stty echo"); print ( "Well done !\n"); } $ntries++; } if ($ntries > 2) { system( "stty echo"); print ( "Sorry, you have failed !\n"); } # This is a simple subroutine. sub rot_13 { ($_) = @_; tr/a-zA-Z/n-za-mN-ZA-M/; return( $_); }
NOTES:
This example has many things that are new. Read every line carefully to understand them.
1. The system function can be used to call any unix function.
2. The logic of the main program is fairly simple, except for the use of the user-defined function. The syntax of defining a function is:
sub functionName { ...statements... }
3. Inside the main function, the function is called using:
do functionName( arguments); 4. The arguments of the function are all copied into an array called @_
This array can then be used in the function to get the parameter values. The last statement of the function is the return value of the function. Explicit use of the return() function can also be made, and is preferred.
5. Note: if you use Perl version 5.0, function calls can be directly made, without using the "do" operator.
Exercises:
1. In the last example, inside the function rot_13 , what is the tr operator doing ?
2. Write a simple perl function to compute the area of a circle of given radius. Use this function from a perl program, which repeatedly asks a user for a radius value, and prints out the area. The main program terminates if the user enters "quit" or "end" or "q".