Perl - Handling Varying Input Formats

One way to handle the fact that the eq operator is an exact equality, is to use a regular expression:  a template that defines a collection of strings that match.  If you've ever used grep or findstr (in NT), then you already know about regular expressions (even if you didn't know that that is what you were using at the time).  The Perl regular expression that matches any string that begins with Larry is ^Larry.  To match this against the string in $name, we use the match operator ( =~ ) as follows:

if ($name =~ /^Larry/) {
	## yes, it matches
}
else {
	## no, it doesn't
)

Note that the regular expression is delimited by slashes.  Within the slashes, spaces and other whitespace are significant, just as they are within strings.

Well, that almost does it!  But what about larry?  To accept larry, we add the ignore-case option, a small i appended after the closing slash - /^Larry/i.

This addition almost meets our needs, but it still doesn't handle rejecting larrys.  To reject larrys, we add a word boundary special marker in the form of \b in the regular expression.  This insures that the character following the y in the variable being matched by the regular expression is not another letter.  This also changes the regular expression to be /^Larry\b/i, which means "Larry at the beginning of the string, no letter or digit following, and OK to be in either case."

When put together with the rest of the program, it looks like this:

#!/usr/bin/perl -w
%words = qw(
	Jason	camel
	Michele	llama
	Tracey	alpaca
	Karee	pea
	Patrick	alpaca
);
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
if ($name =~ /^Larry\b/i) {
	print "Hello, Larry!  How good of you to be here!\n";
}
else {
	print "Hello, $name!\n"; 			# ordinary greeting
	$secretword = $words{$name};			# get the secret word
	if ($secretword eq "") {			# oops, not found
		$secretword = "mfd";			# now, there's a secret word
	}
	print "What is the secret word? ";
	$guess = <STDIN>;
	chomp ($guess);
	while ($guess ne $secretword) {
		print "Wrong, try again.  What is the secret word? ";
		$guess = <STDIN>;
		chomp ($guess);
	}						# end of while not correct
	print "Good guess!  Have a great day!!";
}

As you can see, now the program is a far cry from the simple Hello, world, but it's still very small and workable, and does quite a bit for being so short.  This is the Perl Way.

Perl provides every regular expression feature found in every standard UNIX utility (and even some non-standard ones).  Not only that, but the way Perl handles string matching is about the fastest on the planet, so you don't lose performance.  (A grep-like program written in Perl often beats the vendor-supplied C-coded grep for most inputs.)

So, now I can enter Larry Boatman or larry or Larry, but what about everyone else?  Jason still has to enter exactly Jason (not even Jason followed by a space).  Thus, your task for the next lesson is to determine what programming elements are required to accomplish the task of making the program fair to everyone else.  See you next lesson!