SPUG: regarding Perl doubt

Michael R. Wolf MichaelRWolf at att.net
Mon Jan 26 19:39:15 PST 2009

On Jan 25, 2009, at 11:41 AM, John W. Krahn wrote:

> battipatisainagendra Bhavaniprasad wrote:
>> Hi,
> Hello,
>> I am new to this community.I have question regarding Pattern Matching
>> in Perl.
> In English, when you ask a question, you end the sentence with a  
> question mark.  For example:
> How do you ask a question?

To (my interpretation of) the tone of the answer...

     Did you parse that sentence as a question?  I parsed it as a  
declarative statement, appropriately terminated with a period.

     Larry said "It should be OK to speak baby Perl".  It was meant to  
apply to a apply to the community in addition to the language.  All  
languages are learned by immersing a less-than fluent speaker in a  
fluent-speaker conversation.  It's how we all learn all languages,  
spoken and executable.

To the original question...

In my lightening talk last month, I spoke of Test-Driven Training.   
Using Test::More may be a bit more scaffolding than you want, but I  
have found it to be helpful to my students (and also myself) to set up  
a little experiment to exercise the kinds of edge cases and boundary  
conditions you're trying to understand and declare.  What's more, I've  
found that it's easier to point at a list of test cases and counter- 
cases than it is to describe it in a "natural language", since spoken  
languages aren't really as good at the formal mathematical language  
that's implied by the regex.

#! /usr/bin/perl

use Test::More qw(no_plan);

my $re = qr/ab?/;

like('a', 	$re, 'match a');
like('ab', 	$re, 'match ab');
like('abb', 	$re, 'match ab, but leave the following b alone');
like('abbb', 	$re, 'match ab, but leave the following bb alone');

like('xxxa', 	$re, 'match a floated after some initial non-a');
like('xxxab', 	$re, 'match ab floated after some initial non-ab');

like('axxx', 	$re, 'match a floated before some initial non-a');
like('abxxx', 	$re, 'match ab floated before some initial non-ab');

If this kind of test script doesn't help you, there are lots of online  
animations that run a regex against a test input.  You type a regex in  
one window and some test cases in another window.  Be careful,  
however.  Many of them are a bit "fragile" (say buggy), and some of  
them implement non-Perl regex's.  They're similar to, but not  
identical to, Perl with respect to the features and syntax of  
regex's.  Nevertheless, they're instructive if you understand what  
they do (and do not) do.

I found this one (http://osteele.com/tools/reanimator/) a few years  
ago.  Just now, I found another one (http://regex.powertoy.org/) that  
helps in a different way.  Play.  Learn.

If this doesn't help, come back with a list of strings that you'd like  
to match and ones that you'd like to *not* match.  That list is a  
great way to start a test-driven discussion, of a test-driven script,  
that will lead to a well-tested regex in your code.


Michael R. Wolf
     All mammals learn by playing!
         MichaelRWolf at att.net

More information about the spug-list mailing list