O'Reilly logo

Learning Perl Student Workbook, 2nd Edition by brian d foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15. Smart Matching and given-when

When we first wrote Learning Perl, Fifth Edition, the Perl developers had just added the smart match feature to Perl 5.10, and the book covered up to 5.10.0. After we published our book, however, the developers had to change the smart match behavior because it was fundamentally flawed. In the book, we told you that the smart match operator is commutative, meaning that it doesn’t matter which order you use the arguments. That’s different in Perl 5.10.1 and later. Now the order of arguments matter. We updated this for Learning Perl, Sixth Edition, but you should be careful about your Perl versions and their documentation.

Additionally, some of the rules about how the smart match operator recognizes numbers may have changed. However, we also tell you to look at the table of smart match precedence in the perlsyn documentation, and that’s still the best advice since that will always have the right answer for your version of Perl.

15.1. [Perl 5.10.1] Use the smart match operator in a program that prompts the user for a string and reports if that string is an element of an array you specify in your program. You only have to match exact elements.

If you can’t think of a list of values to put into your array, use the Flintstones characters:

my @array = qw(Fred Wilma Barney Betty Larry Pebbles Bamm-Bamm);

It might help you to remind yourself which values you have in your array. Here are some sample runs:

$ perl ex15.1.pl The elements are (Fred Wilma Barney ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required