Skip Menu |

This queue is for tickets about the Regexp-Grammars CPAN distribution.

Report information
The Basics
Id: 68600
Status: open
Priority: 0/
Queue: Regexp-Grammars

People
Owner: Nobody in particular
Requestors: kolibrie [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: objrule does not work with Moose nor AUTOLOAD
Date: Wed, 01 Jun 2011 13:35:26 -0400
To: bug-regexp-grammars [...] rt.cpan.org
From: Nathan Gray <kolibrie [...] cpan.org>
I am not able to use Moose objects for objrule results. I get strange results when I use classes that use AUTOLOAD. The module works correctly when I use the new method taken from data_structure.t in the Regexp::Grammars distribution, or if I use Class::Accessor objects. I am attaching several test scripts: new.t - based on data_structure.t in the distribution 1..2 ok 1 - Matched ok 2 - Returned correct data structure moose.t - use a Moose object 1..2 Segmentation fault class_accessor.t - use a Class::Accessor object 1..2 ok 1 - Matched ok 2 - Returned correct data structure new_init.t - use a hand-coded object that populates each attribute via an init routine 1..2 ok 1 - Matched ok 2 - Returned correct data structure new_init_autoload.t - uses AUTOLOAD for all accessor methods 1..2 Segmentation fault new_init_limited_autoload.t - uses AUTOLOAD only for accessors that have been listed as allowed 1..2 ok 1 - Matched not ok 2 - Returned correct data structure # Failed test 'Returned correct data structure' # at t/new_init_limited_autoload.t line 75. # Structures begin differing at: # $got->{} = '� �� �� � ' # $expected->{} = '<a href="/yn2011/user/1613">Nathan Gray # (&lrm;kolibrie&lrm;)' # Looks like you failed 1 test of 2. new_init_limited_autoload_warn.t - identical, but has a warn statement 1..2 running AUTOLOAD as 'name' with param 'Nathan Gray' running AUTOLOAD as 'id' with param '1613' running AUTOLOAD as 'alias' with param 'kolibrie' Segmentation fault I am mostly interested in getting Moose to work with Regexp::Grammars. I wrote the other test scripts to try to debug what is going on. When I run moose.t through the Perl debugger, I'm not able to see where the error is, mostly it is just a bunch of reeval messages: DB<2> s main::((reeval 148)[t/moose.t:34]:3): 3: DB<2> s main::((reeval 149)[t/moose.t:34]:2): 2: DB<2> s Signal SEGV at (reeval 149)[t/moose.t:34] line 2 Aborted Tested with: Perl v5.10.1 Regexp::Grammars 1.012 Moose 2.0007 Class::Accessor 0.34 -kolibrie

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Message body is not shown because sender requested not to inline it.

Subject: Re: [rt.cpan.org #68600] objrule does not work with Moose nor AUTOLOAD
Date: Thu, 2 Jun 2011 12:07:36 +1000
To: bug-Regexp-Grammars [...] rt.cpan.org
From: Damian Conway <damian [...] conway.org>
Hi Nathan, Thanks for the excellent bug-report, and especially for the numerous .t files, all of which have now been added to the distribution. Show quoted text
> I am not able to use Moose objects for objrule results.  I get > strange results when I use classes that use AUTOLOAD.
Both the problems you experienced stem from a single cause: the Perl regex engine is not re-entrant (at least, not prior to 5.14.0). The Moose problem you encountered is caused by the fact that some 'isa' type constraints are implemented by testing against regexes. In this case the 'isa' => 'INT' is the culprit. Changing it to something that isn't tested by regex (e.g. to: 'isa' => 'NUM') solves the problem. In the case of the various AUTOLOAD examples, the problem was that your AUTOLOAD() methods included a substitution: $var =~ s/.*://; Changing that line to the non-regex-based equivalent: my $last_colon_pos = rindex($var, ':'); substr $var, 0, $last_colon_pos+1, q{}; removes the problem for all the examples. The bottom line is that this is a perl issue, not a Regexp::Grammars issue. To solve it, you either have to avoid any class that uses regexes in its constructor, or you have to upgrade to 5.14.0. I have now added a new section to the documentation pointing out this inherent limitation of older Perl's: =head4 An important caveat regarding OO rules Prior to Perl 5.14.0, Perl's regex engine was not fully re-entrant. This means that in older versions of Perl, it is not possible to re-invoke the regex engine when already inside the regex engine. This means that you need to be careful that the C<new()> constructors that are called by your object-rules do not themselves use regexes in any way, unless you're running under Perl 5.14 or later (in which case you can ignore what follows). The two ways this is most likely to happen are: =over =item 1. If you're using a class built on Moose, where one or more of the C<has> uses a type constraint (such as C<'Int'>) that is implemented via regex matching. For example: has 'id' => (is => 'rw', isa => 'Int'); The workaround (for pre-5.14 Perls) is to replace the type constraint with one that doesn't use a regex. For example: has 'id' => (is => 'rw', isa => 'Num'); Alternatively, you could define your own type constraint that avoids regexes: use Moose::Util::TypeConstraints; subtype 'Non::Regex::Int', as 'Num', where { int($_) == $_ }; no Moose::Util::TypeConstraints; # and later... has 'id' => (is => 'rw', isa => 'Non::Regex::Int'); =item 2. If your class uses an C<AUTOLOAD()> method to implement its constructor and that method uses the typical: $AUTOLOAD =~ s/.*://; technique. The workaround here is to achieve the same effect without a regex. For example: my $last_colon_pos = rindex($AUTOLOAD, ':'); substr $AUTOLOAD, 0, $last_colon_pos+1, q{}; =back Note that this caveat against using nested regexes also applies to any code blocks executed inside a rule or token (whether or not those rules or tokens are object-oriented). Thanks again for bringing this to my attention, Nathan. Damian
Subject: Re: [rt.cpan.org #68600] objrule does not work with Moose nor AUTOLOAD
Date: Thu, 02 Jun 2011 05:21:16 -0400
To: "damian [...] conway.org via RT" <bug-Regexp-Grammars [...] rt.cpan.org>
From: Nathan Gray <kolibrie [...] cpan.org>
On Wed, Jun 01, 2011 at 10:08:27PM -0400, damian@conway.org via RT wrote: Show quoted text
> Thanks for the excellent bug-report, and especially for the numerous .t files, > all of which have now been added to the distribution.
Thank you for the very clear new documentation, and swift turnaround. -kolibrie