in reply to Dumping regexp for Perl versions earlier than 5.14

In a previous reply of mine to Eily, I pouted that you had given no example code in your OP to illustrate the problem you were asking about. I then realized that you had, in fact, given a link to the module in question, DateTime::Format::Alami, which might be considered a perfect example since it contains all the code!

I've since taken a quick look at the module, and I must admit I still can't see how the change in regex stringification pre/post version 5.10 bears upon what you're trying to achieve. I see that you are building regexes from string fragments, but I don't see where and how you are 'inserting' a regex into another. Can you please clarify these points for me, perhaps with a short, runnable, standalone code example or two?

  • Comment on Re: Dumping regexp for Perl versions earlier than 5.14

Replies are listed 'Best First'.
Re^2: Dumping regexp for Perl versions earlier than 5.14
by perlancar (Hermit) on Oct 03, 2014 at 15:05 UTC

    OK, let's forget my original problem because it is already solved (Eily pointed out that before I convert a string into Regexp object, I can just print it first, and in my case it's true, all I have is a string). But sometimes I do have a Regexp object, for example in my other use-case the validation specification already contains a Regexp object, for example:

    $foo_spec = { args => { arg1 => {required=>1, match=>qr/\A\w+\z/}, arg2 => {required=>1}, }, };
    Now the validator code generator must generate this string:
    defined($arg1) or die "arg1 must be defined";
    $arg1 =~ qr/\A\w+\z/ or die "arg1 does not match regex";
    defined($arg2) or die "arg2 must be defined";
    

    The spec can be written by others.

    Of course, I can require the 'match' key to have a value of string and not Regexp object, so this avoids the problem.

    But I'm interested in knowing whether a newer Perl (5.14+) can stringify a Regexp object in a way that is compatible with older Perls.

Re^2: Dumping regexp for Perl versions earlier than 5.14
by perlancar (Hermit) on Oct 03, 2014 at 15:10 UTC

    Actually, my original problem with DateTime::Format::Alami is not a problem at all, because in my module I never convert to Regexp object. Sorry about that!

    I have reiterated the problem in the first post.

Re^2: Dumping regexp for Perl versions earlier than 5.14
by perlancar (Hermit) on Dec 31, 2014 at 05:15 UTC
    Correction, it's pre/post 5.14 (perl5140delta gives the details).

      I still don't understand your use-case well enough to see why the  "(?adlupimsx-imsx)" embedded pattern-match modifier would not be useful here (see Extended Patterns in perlre).


      Give a man a fish:  <%-(-(-(-<

        OK, let me try again. If I do this on perl 5.14+:
        use 5.010;
        open my($fh), ">", "out";
        my $re = qr/a/;
        say $fh "use 5.010; if ('a' =~ /$re/) { say 'match' } else { say 'no match' }";
        Then out will contain:
        use 5.010; if ('a' =~ /(?^:a)/) { say 'match' } else { say 'no match' }

        This code won't run on perl 5.12 or lower due to unknown regex sequence (?^...), which is introduced in perl 5.14. When run on perl 5.12 or perl 5.10, it will print this error message:

        Sequence (?^...) not recognized in regex; marked by <-- HERE in m/(?^ <-- HERE :a)/ at out line 1.

        I want a code generator that, even though run from perl 5.14+, can generate code that runs on perl 5.12 and lower. So basically I now need to do something like this:

        use 5.010;
        use re qw(regexp_pattern);
        open my($fh), ">", "out";
        my $re = qr/a/;
        my ($pat, $mod) = regexp_pattern($re);
        say $fh "use 5.010; if ('a' =~ /(?$mod-)$pat/) { say 'match' } else { say 'no match' }";
        Note that I left out the part where I need to: 1) remove or complain about regex modifiers that are new in 5.14+ and unrecognized in older perls; 2) escape slashes in pattern. But the main point is there.

        UPDATE: I'm packaging the routine as a CPAN module: Regexp::Stringify