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

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.