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

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

by deprecated (Priest)
on May 01, 2001 at 19:58 UTC ( #77011=perlquestion: print w/replies, xml ) Need Help??

deprecated has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Using arrays of qr!! to simplify larger RE's for readability.
by merlyn (Sage) on May 01, 2001 at 20:00 UTC
    Change your outer brackets to parens. You are creating a list, one element long, which contains an array ref, containing your actual array.

    -- Randal L. Schwartz, Perl hacker

Re: Using arrays of qr!! to simplify larger RE's for readability.
by DrZaius (Monk) on May 01, 2001 at 20:18 UTC
    Turn the square braces to round ones around that are around the regex. You are assigning a list ref to the first element of the list.

    Also, I'm not too sure, but you may want to compile your regex outside of the subroutine. I may be wrong, but my instinks are telling me that qr!! will probably recompile the regex every time it is compiled.

    Am I wrong?

Re: Using arrays of qr!! to simplify larger RE's for readability.
by sachmet (Scribe) on May 01, 2001 at 20:39 UTC
    #!/usr/bin/perl print isend("#---end"); print "\n"; 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; }
    prints solely a newline.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://77011]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-04-17 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?