Though when I write to a file and run it looks like something ...
How are you writing the hash? Data::Dumper? How have you determined the regex looks like qr/(?-xism:(?-xism:^something$))?
I think what you see is just an artifact of regex stringification. I.e., when the regex is stored to the file, it is stringified with the flags being added:
use Data::Dumper; my %myhash; $myhash{REGEXP} = qr/^something$/; print Dumper \%myhash; __END__ $VAR1 = { 'REGEXP' => qr/(?-xism:^something$)/ };
When you then load it again, qr/(?-xism:^something$)/ is being eval'ed. And if you stringify the resulting regex again, another set of flags is being added... Or put differently:
$myhash{REGEXP} = qr/^something$/; print $myhash{REGEXP}; # --> (?-xism:^something$) $myhash{REGEXP} = qr/(?-xism:^something$)/; print $myhash{REGEXP}; # --> (?-xism:(?-xism:^something$)) $myhash{REGEXP} = qr/(?-xism:(?-xism:^something$))/; print $myhash{REGEXP}; # --> (?-xism:(?-xism:(?-xism:^something$))) ...
The multiple flags are certainly redundant, but I'm not sure this is something to worry about.
Update: BTW, YAML also stores the regex stringified (i.e. including flags)
--- REGEXP: !!perl/regexp (?-xism:^something$)
but when you look at the class YAML::Type::regexp (in YAML/Types.pm) which is responsible for loading regexes, you'll see that YAML::Type::regexp::yaml_load() does quite a bit more work than just eval'ing the regex:
package YAML::Type::regexp; ... use constant _QR_TYPES => { '' => sub { qr{$_[0]} }, x => sub { qr{$_[0]}x }, i => sub { qr{$_[0]}i }, ... msix => sub { qr{$_[0]}msix }, }; sub yaml_load { my $self = shift; my ($node, $class) = @_; return qr{$node} unless $node =~ /^\(\?([\-xism]*):(.*)\)\z/s; my ($flags, $re) = ($1, $2); $flags =~ s/-.*//; my $sub = _QR_TYPES->{$flags} || sub { qr{$_[0]} }; my $qr = &$sub($re); bless $qr, $class if length $class; return $qr; }
In other words, it extracts any flags from the stringified regex, and puts them back "outside of" qr{}, i.e.
REGEXP: !!perl/regexp (?i-xsm:^something$) | v qr{^something$}i
The respective flag combinations are stored as subs in a lookup table _QR_TYPES. This way, the above problem of doubling the flags is avoided.
In reply to Re: Generating and storing regexp
by Eliya
in thread Generating and storing regexp
by noodleish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |