1 |
#!
usr/local/bin/perl5 # file four |
2 |
print "Here
are three colors: Red, White, Blue\n"; print "Guess
which one is special\n"; |
3 |
$guess =
<STDIN>; print "You
guessed $guess\n"; |
4 |
if ($guess =~
/\bred\b/i) |
5 |
{print "That is right!\n";} |
6 |
else |
7 |
{print "That is wrong\n";} |
Comments:
/red/
This is a pattern of characters to be searched for in a string. Regular expressions are enclosed in slashes.
\b
A word-boundary pattern anchor.
/\bdog/ matches only those words beginning with dog
“dogfood” =~ /\bdog/ true
/dog\b/ matches only those words ending with dog
“hotdog” =~ /dog\b/ true
/\bdog\b/ matches only those words beginning and ending with dog
/i
A pattern matching option ignoring upper and lower case.
($guess =~ /\bred\b/i)
Evaluates to true if $guess is a word that begins with red, ends with red, regardless of upper and lower case.