Argel has asked for the wisdom of the Perl Monks concerning the following question:
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?#!/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,
(?<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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Read in a regex without having to use string eval?
by ikegami (Patriarch) on Nov 23, 2011 at 01:13 UTC | |
by Argel (Prior) on Nov 23, 2011 at 01:21 UTC | |
by ikegami (Patriarch) on Nov 23, 2011 at 01:32 UTC | |
by Argel (Prior) on Nov 23, 2011 at 02:17 UTC |