如何从 C 在 Perl 5.12 中创建 qr// ?

发布于 2024-08-29 13:25:19 字数 563 浏览 5 评论 0原文

这在 5.8 和 5.10 中对我有用,但在 5.12 中,我的代码创建了这个奇怪的非 qr 对象:

# running "print Dumper($regex)"
$VAR1 = bless( do{\(my $o = '')}, 'Regexp' );

而打印 qr// 不是由我的代码创建的看起来像这样:

# running "print Dumper(qr/foo/i)"
$VAR1 = qr/(?i-xsm:foo)/;

我的代码基本上是:

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);

任何人都知道如何正确创建5.12 中字符串的正则表达式?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-09-05 13:25:19

谢谢你们让我走上正轨,伙计们,事实证明我想得太多了。他们只是剪掉了魔法线,并没有创建PV。

这就是您在 Perl 5.12 中需要做的全部事情:

REGEXP *rx = re_compile(pattern, flags);
SV *regex = newRV((SV*)rx);

stash = gv_stashpv("Regexp", 0);
sv_bless(regex, stash);

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);
趴在窗边数星星i 2024-09-05 13:25:19

看看 这个由霍布斯回答。为了方便阅读,我将其复制如下:

Regex 对象实际上在 5.12.0 中变得更加“核心”,因为它们现在是对 REGEXP 类型的标量的引用,而不是对带有魔法的标量的引用。然而,这对于用户代码来说是完全不可见的,除非您设法绕过重载的字符串化,在这种情况下您会注意到正则表达式现在打印为 Regexp=REGEXP(0x1234567) 而不是 Regexp=SCALAR(0x1234567)

我不是特别喜欢熟悉 XS,但我怀疑您不能再使用标量值来创建正则表达式。

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.

剪不断理还乱 2024-09-05 13:25:19

Perl 5.12 将正则表达式更改为一流对象,您可以在 如何检查标量中是否包含已编译的正则表达式?

我不是 XS 人,所以我不知道您需要在代码中更改哪些内容才能使其正常工作。在 perl 源代码中搜索“REGEXP”会显示他们对核心模块进行的修复以使用新内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文