in reply to Re^2: (??{ code }) versus (?PARNO) for recursive regular expressions
in thread (??{ code }) versus (?PARNO) for recursive regular expressions

Still throws a strictures warning.

Of course. Variable declarations includes our.

These work:

1. no strict; $x = qr/(??{ $x })/; 2. use strict; our $x = qr/(??{ our $x })/ 3. use strict; our $x = qr/(??{ no strict; $x })/ 4. use strict; our $x; $x = qr/(??{ $x })/ 5. use strict; my $x; $x = qr/(??{ $x })/

I think there was once a bug where some or all pragmas didn't propagate into (??{ }) and the like. If so, the following would have worked because it would have been equivalent to #3 above:

6. use strict; our $x = qr/(??{ $x })/

The earliest I have here is 5.10.1, and it doesn't have this bug.

Replies are listed 'Best First'.
Re^4: (??{ code }) versus (?PARNO) for recursive regular expressions
by wind (Priest) on Mar 25, 2011 at 23:32 UTC

    Now I'm definitely going down the old memory train. When I first used the (??{ code }) feature, we had to declare our globals using vars.

    use vars qw($x_re); $x = qr/... (??{ $x }) .../

    This of course worked, but we were quite excited when they released our with perl561delta. Near that point in time, the following did work as desired without throwing errors:

    use strict; our $x = qr/... (??{ $x }) .../

    The thing as, as I understand the documentation of perlre, it should be allowed

    This is a "postponed" regular subexpression. The code is evaluated at run time, at the moment this subexpression may match.

    The whole point of being able to do a recursive regular expression is that you can include the qr// regex that you're currently defining. However, given the choices you listed above, I'd probably go with the following even if it does feel messy

    our $x = qr/... (??{ our $x }) .../

    Anyway, putting all that aside, I'd still like to get back to my primary question. Is this a feature that I should keep in my toolkit, or should I just drop it in favor of (?PARNO)? I can't think of any situation currently where I'd need the former over the latter, but I would like to know if y'all would consider still using it or not.

      The thing as, as I understand the documentation of perlre, [use strict; our $x = qr/... (??{ $x }) .../ ] should be allowed

      You're using a variable ($x) you haven't declared yet. It's a strict error just like

      $ perl -e'use strict; our $x = $x;' $

      huh... what? I guess I was wrong. our declarations take effect immediately. I don't see why «our $x = $x;» and «our $x = qr/(??{ $x })/;» can behave differently. It would seem there's a bug here.

        Thank you for the sanity check. I ran your above statements on strawberry perl v5.12.1

        > perl -e"use strict; our $x = $x;" > perl -e"use strict; our $x = qr/(??{ $x })/;" Variable "$x" is not imported at (re_eval 1) line 2. Global symbol "$x" requires explicit package name at (re_eval 1) line +2. Compilation failed in regexp at -e line 1. > perl -e"use strict; my $x = qr/(??{ $x })/;" Global symbol "$x" requires explicit package name at (re_eval 1) line +2. Compilation failed in regexp at -e line 1.

        And on perl v5.10.0 built for i686-linux

        $ perl -e'use strict; our $x=$x;' $ perl -e'use strict; our $x=qr/(??{ $x })/;' Variable "$x" is not imported at (re_eval 1) line 2. Global symbol "$x" requires explicit package name at (re_eval 1) line +2. Compilation failed in regexp at -e line 1. $ perl -e'use strict; my $x = qr/(??{ $x })/;' Global symbol "$x" requires explicit package name at (re_eval 1) line +2. Compilation failed in regexp at -e line 1.

        However, done on perl v5.8.0 built for i386-linux-thread-multi, I get no strictures errors and the original parsing code works as desired.:

        $ perl -e'use strict; our $x = $x' $ perl -e'use strict; our $x = qr/(??{ $x })/;' $ perl -e'use strict; my $x = qr/(??{ $x })/;'

        So this change to the way (??{ code }) was validated for strictures changed at least after v5.8.0. Maybe this was done when (?PARNO) was introduced in v5.10.0. I guess that's one way to encourage people to move to a new feature. Gotta say it's still annoying though.

        Update: Added data on my declarations using (??{ code }) per ikegami's request.