As some of you may know, I've been grappling with using RE's (which I really prefer to use) in code rather than conditionals which are much easier to read (but ten times slower).

I have come up with a neat idea that I thought would simplify my code, and also still allow me to use regular expressions. Rather than having one 60 character RE handle things, I can break stuff down into smaller RE's and put them in an array and then iterate over them. So while I am not using the full extent of the RE engine, I am also not making perl do backflips for conditionals -- and all my co-workers will be able to read it (because theyre small and simple).

So an example of this is the following sub:

sub isend { my $line = shift; my @terminators = [ qr!#=3D=3D=3D(?:END|end)!, qr!#===(?:END|end)!, qr![-]+_=_NextPart_!, qr!1===(?:END|end)!, qr!#---(?:END|end)!, ]; foreach my $ending (@terminators) { return undef if $line =~ $ending; } return 1; }
Note how cool those qr!!'s line up and are easily readable by almost anyone (yeah I'm proud of myself. *grin*). However, the test is evaluating true every single time, even when there is no reason it should be. When I print them out, I get an array ref, which does seem odd since that would seem to me to resemble this:
@terminators = [ [foo, bar], [baz, bletch] ]; # an array of array refs, right?
rather than what it really is, above. So I'm sure I am missing something stupid here, and I'd really like to use this construct since it seems like such a magical silver bullet for my readbility vs. speed dilemma.

thanks
brother dep.

--
Laziness, Impatience, Hubris, and Generosity.


In reply to Using arrays of qr!! to simplify larger RE's for readability (code). by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.