Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found