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 127.0.0.9 CisACS_05_TACACSAdm +in dsg4te2 1 0 User-Name=XXXXXX,NAS-IP-Address=127.0.0.10,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?
(?<NASPRE>\QNAS-IP-Address=\E)

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


In reply to Read in a regex without having to use string eval? by Argel

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.