简体   繁体   中英

Perl next if regex stored in array

i have a piece of code with aa while loop and a couple of next if's like this in the loop :

LOOP:
while (something) {
   next if $UPUPRF =~ /^T[0-9]{6}/;
   next if $UPUPRF =~ /^SECURITE.*/;
   next if $UPUPRF =~ /^AUDIT[A-Z]{2}/;
}

i would like to put these regexes in my configuation file (a perl data structure) instead of having them "hardcoded" in the script, so that the user can edit them easily by just editing the configuration file, and would like your advice regarding this.

what is the cleanest way of doing this ?

first, i guess i can store the regexes like this in the configuration file ? (with qr//):

 ....
 exclude_acct => [
                    qr/^T[0-9]{6}/,
                    qr/^AUDIT[A-Z]{2}/,
                    qr/^SECURITE.*/,
                 ],
 ....

and in my loop, should i use something like this ? :

foreach (@{ $myhash{exclude_acct} }) {
    next LOOP if $UPUPRF =~ /$_/;
}

thanks for your advices.

regards

note : i haven't tried this code yet, just guessing that i could do it like that, but i'm mainly wondering if this is "clean" or if there is a cleaner way of doing it

That's fine, but starting the regex engine is relatively expensive, so it would be faster than create one regex from the many.

# Once, before the loop.
my $pat = join "|", @{ $myhash{exclude_acct} };
my $re = qr/$pat/;

# In the loop.
next LOOP if $UPUPRF =~ $re;

This can break backreferences ( \\1 ), though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM