Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've just got a warning from CPAN testers about one of my modules failing to compile under Perl 5.8.4.

The code, in its simplest form, is almost the same that you could get from perlre:

#!/usr/bin/perl -w use strict; our $re = qr{ \( (?: (?> [^()]+ ) | (??{ $re }) )* \) }x;

This code used to work fine from Perl 5.6.1 to 5.8.3

Under the latest version (5.8.4) it breaks as follows:

Variable "$re" is not imported at (re_eval 1) line 2.
Global symbol "$re" requires explicit package name at (re_eval 1) line 2.
Compilation failed in regexp at testrecursive.pl line 12.

If I want to compile the same code, now, I need to either enclose the offending regex inside a "no strict 'vars'" block or call the variable with a package name:

#!/usr/bin/perl -w use strict; our $re = qr{ \( (?: (?> [^()]+ ) | (??{ $main::re }) )* \) }x;

With this correction the code compiles.

Proof of concept:

$_ = 'aa bb (cc (dd) ee) ff '; # wanting ^----------^ print "$1\n" while m/($re)/g ; __END__ __OUTPUT__ (cc (dd) ee)

I wonder, though, if the "correct" behavior was the previous one or the current one.

I mean, I can't use this code with a variable declared as "my $re = qr/ ....", because by the time it gets embedded inside the regex, its value is still undefined. The example in perlre is actually using a global variable, which won't work under "strict" rules.

Any thoughts?

update (1)
The previous behavior was "correct" in the technical sense. The compiler wouldn't complain and it would produce the expected results.

I remember trying the my $re; $re = qr//; arrangement in an earlier version of Perl, and discarding it because it wouldn't work. I tried to replicate the failing behavior but I can't isolate a simple case, even though my module (Chess::PGN::Parse) will work as expected with a complete package name, but it will break under this syntax.

My decision of using "our" came after seeing that TheDamian's Regexp::Common was implemented without "use strict" at all. I should have looked at it again after Abigail took over the module.

update (2)
Actually, it will also work with our $re; $re = qr/ ... $re.../; (Thanks dragonchild).

update (3)
I submitted a patched module to CPAN, and now (26-May-2004) the testers reports look much better!

 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Broken compatibility for recursive regex in Perl 5.8.4 by gmax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-16 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found