[Israel.pm] The qr// operator

Gaal Yahas gaal at forum2.org
Tue Dec 30 14:49:50 PST 2008


There are several reasons why you might want to use qr// rather than
create a large string and get it compiled at the last minute.

There's performance, as you mentioned. Sometimes it matters that the
regexp be compiled only once.

You may be getting the regular expression from an external source,
like a config file or the user (imagine an editor with a "find with
regular expression" feature). You want to eval { qr// } the input
immediately, and inform the user of any errors before attempting to do
any matches.

Composing large regexps out of small ones is incredibly useful for
parsing complex languages. This is limited in occasion with Perl 5
regexps(*), because their captures are positional (i.e., you have to
know exactly what to expect in $1, $2 etc.), but can get you very far
with Perl 6 Rules, which are wonderfully compositional. For example,
if you have a Rule parsing a number (and returning a Number object),
and a Rule parsing an arithmetic operation (and returning an Operation
object), you just have to add a third Rule parsing and returning an
Expression, and there you go, you have a grammar for a calculator,
that when run successfully against some input, returns a tree
structure. And this is easily maintainable; if you want your parser to
start supporting new a Operation, you know exactly where to extend it.

(*) Perl 5.10 improves this, though.

On Wed, Dec 31, 2008 at 12:17 AM, Eli Billauer <eli at billauer.co.il> wrote:
> The open question here is about performance. As for readability, the
> example shows pretty clearly that a complex regular expression can be
> made accessible to mortals.
>
>
> And yes, I suppose that gradually creating a complex regular expression
> as a string, with parentheses carefully put in place, would do the job.
> Have you ever seen someone doing that?
>
>
> The point is, that for some reason, setting up a regular expression in
> stages is pretty rare (read: never seen it until now). What is much more
> commonplace is a bombardment of cryptic characters. And I sincerely
> wonder if there is any reason in the world why regular expressions
> should look that way.
>
>
>   Eli
>
>
> Avishalom Shalit wrote:
>
>> isn't qr a performance thing ?
>> couldn't you have gotten the same readability with strings ?
>>
>>
>>
>
>
> --
> Web: http://www.billauer.co.il
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list