[sf-perl] qr{} question

Ronald J Kimball rjk-perl-p5p at tamias.net
Tue May 26 12:41:14 PDT 2009


On Tue, May 26, 2009 at 10:24:16PM +0300, Shlomi Fish wrote:
> On Tuesday 26 May 2009 21:52:20 David Alban wrote:
> > greetings,
> >
> > i'm trying to interpolate a value in a compiled regex.  it's
> > interpolating the first time, but not the second.  that is:
> >
> >     #!/usr/bin/perl
> >
> >     use strict;
> >     use warnings;
> >
> >     use Data::Dumper;
> >     use Readonly;
> >
> >     Readonly my $FOO => 'foo';
> >     Readonly my $FOO_REGEX => qr{ $FOO [^#]+ -D $FOO }xms;
> >
> >     print "\nReadonly:\n";
> >     print '$FOO:       ', Dumper $FOO;
> >     print '$FOO_REGEX: ', Dumper $FOO_REGEX;
> >
> > prints:
> >
> >     Readonly:
> >     $FOO:       $VAR1 = 'foo';
> >     $FOO_REGEX: $VAR1 = qr/(?msx-i: foo [^#]+ -D $FOO
> >     )/;
> >
> >
> 
> That appears to be a bug in perl. (tested on perl-base-5.10.0-25mdv2009.1 ). 
> I'm CCing this to the perl5-porters. Nice catch!
> 

The problem is the combination of /x and [^#].  It appears that the # is
being interpreted both as beginning a comment and as being part of the
regular expression simultaneously.

You should be able to work around this bug by escaping the # with a
backslash.

Ronald


More information about the SanFrancisco-pm mailing list