|
1 |
#!
/usr/local/bin/perl5 # file three |
|
|
2 |
print "Here
are three colors: Red, White, Blue\n"; print "Guess which one is special\n"; |
|
|
3 |
$guess = <STDIN>; |
|
|
4 |
print "Your
input is $guess\n"; |
|
|
5 |
chop($guess); |
|
|
6 |
$guess =~
tr/A-Z/a-z/; |
|
|
7 |
print "In
lower case it is $guess\n"; |
|
|
8 |
if ($guess eq "red") |
|
|
9 |
{print "That is right!\n";} |
|
|
10 |
else |
|
|
11 |
{print "That is wrong\n";} |
|
Comments:
chop($guess);
The chop function removes the last character from a string. Here it removes the newline character from the end of the input line.
=~
A matching operator (i.e., an equals sign, tilde). It is often used with other operators to either find a pattern, substitute a pattern or transform a pattern.
$question =~ /please/ True if the $question contains the string “please”
“Coffee, please” =~ /please/ True
“Tea is pleasent or pleasant” =~ /please/ True
“Please and thank you” =~ /please/ False
$word =~ s/dog/cat/ Substitutes “cat” for “dog” in $word
$word = “dogfood”;
$word =~
s/dog/cat/;
$word eq “catfood”; True
$guess =~ tr/A-Z/a-z/;
This translates all upper-case characters (i.e., A-Z) to lower-case ones (i.e., a-z).
if ($guess eq "red") # A conditional statement
evaluated for its truth
{print "That is right!\n";} # Statements done if conditional is true
else
{print "That is wrong\n";} # Statements done if conditional is false