Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Read in a regex without having to use string eval?

by Argel (Prior)
on Nov 23, 2011 at 00:49 UTC ( #939568=perlquestion: print w/replies, xml ) Need Help??
Argel has asked for the wisdom of the Perl Monks concerning the following question:

In the code below, the first line read in is a RegEx we want to test. What I'm wondering is if there is a way I can eliminate the string eval and just enter the RegEx itself without any additional code on the first line?
#!/usr/local/perl510/bin/perl use feature ":5.10"; use strict; use warnings; use Data::Dumper; my @data; while( <DATA> ) { chomp $_; push @data, $_; } my $tmp = shift @data; my $regex = eval $tmp; print Dumper $regex; print Dumper \@data; foreach my $match_against ( @data ) { $match_against =~ $regex; print Data::Dumper->Dump( [\%+],['%+']); } __DATA__ qr{(?<NASPRE>\QNAS-IP-Address=\E)} Nov 21 15:04:29 TACACS01 Nov 21 21:04:29 CisACS_05_TACACSAdm +in dsg4te2 1 0 User-Name=XXXXXX,NAS-IP-Address=,Group-Name= +XXXXXX,NAS-Portname=tty1,service=shell,cmd=write memory <cr>,priv-lvl +=15,task_id=12344,
I would like to enter the first line in __DATA__ like below, and not have to do a string eval if possible. Is there any easy way to do that?

I know the example regex is pointless -- I'm just using it to illustrate the problem, which is that if I just read the line in normally and do not do a string eval, I get error messages like "Unrecognized escape \Q passed through in regex....". I feel like I'm missing something obvious, so feel free to hit me with a clue stick! (^_^;) (^_^)

Update: To be clear, I started off years ago with this script just reading the line in like I want to, but that breaks things like quotemeta. So I find myself working on this again and I'm wondering if there is a way I can just read it in -- i.e. 'my $regex = shift @data;' and no string eval, and no non-regex code in the first line of ___DATA__?

For the curious, this script is used to test named capture RegEx's for a logging app that uses the PCRE library. Since we have several gigs worth of logs (or probably terabytes by now), testing a RegEx in the app can be painful. This script was intended to allow us to work out most of the kinks in the RegEx before trying to run it in the app.

UPDATE: As ikegami helped me realize, the quotemeta (\Q...\E) is actually Perl code. Knew it was something obvious! Thanks for the help!!

Elda Taluta; Sarks Sark; Ark Arks
My deviantART gallery

Replies are listed 'Best First'.
Re: Read in a regex without having to use string eval?
by ikegami (Pope) on Nov 23, 2011 at 01:13 UTC

    is not a regex pattern. It's a Perl operator, it's Perl code. You need eval EXPR or similar to execute Perl code.

    Now, if you actually had a regexp pattern, say


    there wouldn't be need for eval EXPR or similar.

    chomp( my $pat = <DATA> ); while (<DATA>) { print if /$pat/; } __DATA__ (?-xism:(?<NASPRE>NAS\-IP\-Address\=)) ...
    chomp( my $pat = <DATA> ); my $re = qr/$pat/; while (<DATA>) { print if /$re/; } __DATA__ (?-xism:(?<NASPRE>NAS\-IP\-Address\=)) ...
      I knew I was missing something obvious! I'm so used to using quotemeta in RegEx's that I was thinking it was RegEx code, not Perl code! Thanks! Needed someone to point that out to me!! I'm aware about the rest.

      Elda Taluta; Sarks Sark; Ark Arks
      My deviantART gallery

        I was actually referring to the qr{} part, but yeah, it's basically the same for \Q.

        The deal with the quotemeta escape and such as well as interpolation is that they are meaningful in literals (such as qq"" and qr//), but they are not perceived as such by the regex engine should they be provided to it.

        You can print out the result of qr to see the pattern that got compiled.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939568]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2018-04-21 21:41 GMT
Find Nodes?
    Voting Booth?