in reply to Re: save regex in a file?
in thread save regex in a file?

A slightly different approach using the same idea is to put the qr// statements directly in the file and eval them after reading in. This improves the readability if the file is to be edited by human beings.

So the regex data file would look like this:

qr/abc/i qr/^\d+$/m ...
Reading in and evaling (of course this might introduce some security problems) could then be done this way:
my @regexes; # read in open RE, "< my_re.pl" or die "can't read my_re.pl: $!"; while(<RE>) { chomp; push @regexes, eval; } close RE; # and do the matching for my $re (@regexes) { if ($text =~ $re) { # do something } }

With some extra effort one could even allow multiline regexes in the data file.

-- Hofmator