This has been working for me in 5.8 and 5.10, but in 5.12 my code creates this weird non-qr object:
# running "print Dumper($regex)"
$VAR1 = bless( do{\(my $o = '')}, 'Regexp' );
Whereas printing a qr// not created by my code looks like this:
# running "print Dumper(qr/foo/i)"
$VAR1 = qr/(?i-xsm:foo)/;
My code is basically:
REGEXP *rx = re_compile(pattern, flags);
SV *regex = sv_2mortal(newSVpv("",0));
sv_magic(regex, (SV*)rx, PERL_MAGIC_qr, 0, 0);
stash = gv_stashpv("Regexp", 0);
sv_bless(newRV((SV*)regex), stash);
Anyone know how to correctly create a regex from a string in 5.12?
Thanks for putting me on the right track, guys, it turns out I was seriously overthinking this. They just cut out the magic line and don't create the PV.
This is all you need to do in Perl 5.12:
REGEXP *rx = re_compile(pattern, flags);
SV *regex = newRV((SV*)rx);
stash = gv_stashpv("Regexp", 0);
sv_bless(regex, stash);
Take a look at the comments in this answer by hobbs. I've copied it below for ease of reading:
Regex objects actually get slightly more "core" in 5.12.0, as they're now references to scalars of type REGEXP rather than references to scalars with magic. This is, however, completely invisible to user code, unless you manage to bypass overloaded stringification, in which case you'll notice that regexes now print as Regexp=REGEXP(0x1234567) instead of Regexp=SCALAR(0x1234567)
I'm not especially familiar with XS, but I suspect you can't use a scalar value any more to create your regex.
Perl 5.12 changed regexps to be first class objects, which you find as part of the tangential discussion in How do I check if a scalar has a compiled regex it in?.
I'm not an XS person, so I don't know what you need to change in your code to make it work out. Searching for 'REGEXP' in the perl sources shows the fixes they made to the core modules to use the new stuff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With