[Israel.pm] Fwd: [Telux] Next Meeting: "High-Level Programming Concepts Using Perl 6" on 22-March

Shlomi Fish shlomif at iglu.org.il
Sat Mar 14 05:40:14 PDT 2009

Hi Evgeny!

On Saturday 14 March 2009 01:35:44 Evgeny wrote:
> Oh! (sorry for spamming) ... but I forgot the very first and most important
> thing that I could not do in Perl! :)
> In the original Ruby Cucumber, the "Given"/"When"/"Then" are functions that
> have a regexp and a block. After trying to do the same with Perl, and still
> keeping the %matchers hash simple - I had to put these words inside the
> regexp. And I don't really know how I can concatenate regexps, 

You can concatenate regexps simply by doing:

my $re1 = qr{Hello};
my $re2 = qr{Evgeny};
my $re_total = qr{$re1 $re2};

> or add
> additional things to an existing regexp. 

This is harder with qr//. qr// are already compiled and so they are Perl's 
internal representation of a regex, and not sure how easily manipulated they 
can be. Maybe one can introspect them using one of the B::* modules. Maybe 
Yuval Ya'ari or someone else would be able enlighten us in this respect.

In any case, if you wish to manipulate regexps, you can keep them as strings, 
and only compile them only once you wish to use them. For example: (tested)


use strict;
use warnings;

my $greedy_re_str = "(s.*t)";

my $string = "stumble upon the thames";

my $greedy_re = qr{$greedy_re_str};

if (my @matches = $string =~ $greedy_re)
    print "Found: ", $matches[0], "\n";

my $non_greedy_re_str = $greedy_re_str;

$non_greedy_re_str =~ s{\*}{*?}g;

my $non_greedy_re = qr{$non_greedy_re_str};

if (my @matches = $string =~ $non_greedy_re)
    print "Found: ", $matches[0], "\n";


	Shlomi Fish

> So at the end it was simplified by
> moving the words themselves into the regexp. The other solutions that I
> attempted added complexity and code and duplication, and this is something
> I want to avoid.
> This actually might not be an argument that perl is less dynamic than ruby,
> but for some reason I have this weird feeling that I could do it in ruby
> and I just cant do it in perl. For example using the ruby Regexp.source or
> Regexp.to_s methods to do "more" with regular expression objects.
> -
> evgeny

Shlomi Fish       http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/

God gave us two eyes and ten fingers so we will type five times as much as we

More information about the Perl mailing list